| Crates.io | inplace-iter |
| lib.rs | inplace-iter |
| version | 0.3.0 |
| created_at | 2025-05-27 15:08:19.346928+00 |
| updated_at | 2025-05-29 13:22:57.92185+00 |
| description | Efficient in-place iteration with O(1) element removal and taking operations |
| homepage | https://github.com/anickaburova/inplace-iter |
| repository | https://github.com/anickaburova/inplace-iter |
| max_upload_size | |
| id | 1691229 |
| size | 54,397 |
A Rust library providing iterators that allow efficient in-place modification of collections with O(1) removal and take operations.
loop-lifetime-guard feature (enabled by default)Add this to your Cargo.toml:
[dependencies]
inplace-iter = "0.1"
use inplace_iter::prelude::*;
let mut numbers = vec![1, 2, 3, 4, 5];
for item in numbers.removable_iter() {
if *item.get() % 2 == 0 {
item.remove(); // Efficiently remove even numbers
}
}
// Order is not preserved, but all even numbers are removed
assert_eq!(numbers.len(), 3);
use inplace_iter::prelude::*;
let mut names = vec!["Alice".to_string(), "Bob".to_string(), "Charlie".to_string()];
let mut long_names = Vec::new();
for item in names.takeable_iter() {
if item.get().len() > 4 {
long_names.push(item.take()); // Take ownership of long names
}
}
assert_eq!(long_names, vec!["Alice".to_string(), "Charlie".to_string()]);
assert_eq!(names.len(), 1);
assert_eq!(names[0], "Bob");
use inplace_iter::prelude::*;
let mut numbers = vec![1, 2, 3, 4, 5];
let mut sum = 0;
for mut item in numbers.takeable_iter_mut() {
if *item.get() > 3 {
// Take ownership of elements > 3
sum += item.take();
} else {
// Double odd numbers
*item.get_mut() *= 2;
}
}
assert_eq!(sum, 9); // 4 + 5
assert_eq!(numbers.len(), 3);
assert_eq!(numbers, vec![2, 4, 6]);
use inplace_iter::prelude::*;
let mut numbers = vec![1, 2, 3, 4, 5];
let mut confirm = numbers.removable_confirm_iter();
for item in confirm.iter() {
if *item.get() % 2 == 0 {
item.remove(); // Efficiently remove even numbers
}
}
// Multiple calls to `iter()` are allowed, and the subsequent iterations will not yield the removed elements.
let next_iter = confirm.iter().map(|i| *i.get()).collect::<Vec<_>>();
assert_eq!(next_iter, vec![1,5,3]);
confirm.confirm_removals();
assert_eq!(numbers, vec![1, 5, 3]);
use inplace_iter::prelude::*;
let mut numbers = vec![1, 2, 3, 4, 5];
let mut confirm = numbers.removable_confirm_iter();
for item in confirm.iter() {
if *item.get() % 2 == 0 {
item.remove(); // Efficiently remove even numbers
}
}
confirm.cancel_removals();
// Order after cancellation is not guaranteed. Also, if used on mutable iterator, the elements will stay
// modified after cancelling!
assert_eq!(numbers, vec![1, 5, 3, 4, 2]);
This library uses unsafe code internally to provide its functionality. While the API is designed to be safe, incorrect usage can lead to undefined behavior. Always follow these guidelines:
Don't hold multiple mutable references to the same element
Don't use an item after it has been removed/taken
The loop-lifetime-guard feature (enabled by default) adds runtime checks to detect invalid item usage
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
iter_mut() variants)Confirmation of removals iterator wrapper (mut and const)
Licensed under either of:
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.