for_loop/
for_next.rs

1// File: ./bin-hello/examples/for_loop/for_next.rs
2// clear && cargo run --example for_loop --features ok -- for_next | bat -l cmd
3// clear && cargo run --example for_loop --features err_04
4// clear && cargo run --example for_loop --features err_05
5// clear && cargo run --example for_loop -- for_next
6
7//=======
8
9
10//=======
11#[cfg(feature = "ok")]
12pub fn adjoin() {
13    // ANCHOR: feature-ok
14    // File: ./bin-hello/examples/for_loop/for_next.rs
15    // #[cfg(feature = "ok")]
16
17    let mut instance = vec![33u8; 5];
18    let mut iter = instance.iter_mut();
19    
20    loop {
21        match iter.next() {
22            Some(x) => println!("{:?}", x),
23            None => break,
24        }
25    }
26    
27    println!("{:?}", instance);
28
29    // ANCHOR_END: feature-ok
30}
31
32
33
34//=======
35#[cfg(feature = "cp")]
36pub fn adjoin() {
37    // ANCHOR: feature-cp
38    // File: ./bin-hello/examples/for_loop/for_next.rs
39    // #[cfg(feature = "cp")]
40
41    let mut instance = vec![33u8; 5];
42    
43    for index in 0..instance.len() {
44        instance[index] = (index as u8) + instance[index];
45        println!("{:?}", instance[index]);
46    }
47
48    println!("{:?}", instance);
49
50    // ANCHOR_END: feature-cp
51}
52
53
54
55
56//=======
57#[cfg(feature = "okey")]
58pub fn adjoin() {
59    // ANCHOR: feature-okey
60    // File: ./bin-hello/examples/for_loop/for_next.rs
61    // #[cfg(feature = "okey")]
62
63    let mut instance = vec![33u8; 5];
64    
65    for (index, item) in instance.iter_mut().enumerate() {
66        *item = (index as u8) + *item;
67        println!("{:?}", *item);
68    }
69
70    println!("{:?}", instance);
71
72    // ANCHOR_END: feature-okey
73}
74
75
76
77
78//=======
79#[cfg(feature = "okay")]
80pub fn adjoin() {
81    // ANCHOR: feature-okay
82    // File: ./bin-hello/examples/for_loop/for_next.rs
83    // #[cfg(feature = "okay")]
84
85    let instance: Vec<_> = vec![33u8; 5]
86        .into_iter()
87        .enumerate()
88        .map(|(index, item)| (index as u8) + item)
89        .collect();
90    
91    println!("{:?}", instance);
92
93    // ANCHOR_END: feature-okay
94}
95
96
97
98
99//=======
100#[cfg(feature = "err_07")]
101pub fn adjoin() {
102    // ANCHOR: feature-error_01
103    // File: ./bin-hello/examples/for_loop/for_next.rs
104    // #[cfg(feature = "err_07")]
105
106    let mut instance = vec![33u8; 5];
107    //let mut iter = instance.iter_mut();   // OK
108    let iter = instance.iter_mut();
109    
110    loop {
111        match iter.next() {
112            Some(x) => println!("{:?}", x),
113            None => break,
114        }
115    }
116    
117    println!("{:?}", instance);
118    // ANCHOR_END: feature-error_01
119}
120
121
122
123
124//=======
125#[cfg(feature = "err_08")]
126pub fn adjoin() {
127    // ANCHOR: feature-error_02
128    // File: ./bin-hello/examples/for_loop/for_next.rs
129    // #[cfg(feature = "err_08")]
130
131    let instance = vec![33u8; 5];
132    //let mut iter = IntoIterator::into_iter(instance);
133    let mut iter = instance.into_iter();
134    
135    loop {
136        match iter.next() {
137            Some(x) => println!("{:?}", x),
138            None => break,
139        }
140    }
141    
142    println!("{:?}", instance);
143    
144    // ANCHOR_END: feature-error_02
145}
146
147
148
149//=======
150#[cfg(all(
151    not(feature = "ok"), 
152    not(feature = "okay"),
153    not(feature = "okey"),
154    not(feature = "cp"),
155    not(feature = "err_07"),
156    not(feature = "err_08"),
157))]
158pub fn adjoin() {
159    use aide::hello;
160    hello();
161}
162
163// https://www.reddit.com/r/rust/comments/61x2yd/idiomatic_way_to_handle_modifying_vectors_in_a/