for_loop/
for_enumerate.rs

1// File: ./bin-hello/examples/for_loop/for_enumerate.rs
2// clear && cargo run --example for_loop --features ok -- for_enumerate | 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_enumerate
6
7//=======
8
9
10//=======
11#[cfg(feature = "ok")]
12pub fn adjoin() {
13    // ANCHOR: feature-ok
14    // File: ./bin-hello/examples/for_loop/for_enumerate.rs
15    // #[cfg(feature = "ok")]
16
17    let instance: Vec<_> = vec![33u8; 5]
18        .into_iter()
19        .enumerate()
20        .map(|(index, item)| {
21            let ret = (index as u8) + item;            
22            println!("{:?}", ret);
23            ret
24        })
25        .collect();
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_enumerate.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_enumerate.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_enumerate.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//=======
101#[cfg(all(
102    not(feature = "ok"), 
103    not(feature = "okay"),
104    not(feature = "okey"),
105    not(feature = "cp"),
106))]
107pub fn adjoin() {
108    use aide::hello;
109    hello();
110}
111
112// https://www.reddit.com/r/rust/comments/61x2yd/idiomatic_way_to_handle_modifying_vectors_in_a/