Skip to content

Commit 3dd57d7

Browse files
authored
Add doc that explains how to mutate IArray (#63)
Closes #17 Closes #34
1 parent 57baf1e commit 3dd57d7

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Diff for: src/array.rs

+21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@ use crate::ImplicitClone;
77
///
88
/// This type is cheap to clone and thus implements [`ImplicitClone`]. It can be created based on a
99
/// `&'static [T]` or based on a reference counted slice (`T`).
10+
///
11+
/// Since `IArray<T>` is an immutable data structure, direct modifications like adding or removing
12+
/// elements are not possible. To make changes, you need to convert it into a `Vec<T>` using
13+
/// `.to_vec()`, modify the vector, and then convert it back into an `IArray<T>` using
14+
/// `IArray::from`. Here's an example demonstrating this approach:
15+
///
16+
/// ```rust
17+
/// # use implicit_clone::unsync::*;
18+
/// let iarray = IArray::from(vec![1, 2, 3]);
19+
///
20+
/// // Convert to Vec, modify it, then convert back to IArray
21+
/// let mut vec = iarray.to_vec();
22+
/// vec.push(4);
23+
/// vec.retain(|&x| x != 2); // Remove the element `2`
24+
/// let new_iarray = IArray::from(vec);
25+
///
26+
/// assert_eq!(new_iarray, IArray::from(vec![1, 3, 4]));
27+
/// ```
28+
///
29+
/// This ensures that you can work with a mutable `Vec<T>` while still benefiting from
30+
/// `IArray<T>`'s immutable properties when needed.
1031
#[derive(PartialEq, Eq)]
1132
pub enum IArray<T: ImplicitClone + 'static> {
1233
/// A static slice.

0 commit comments

Comments
 (0)