mut_var_sized/string_ref.rs
1// File: ./examples/mut_var_sized/string_ref.rs
2// clear && cargo run --example mut_var_sized --features ok -- string_ref
3// clear && cargo run --example mut_var_sized --features cp -- string_ref
4// clear && cargo run --example mut_var_sized --features err_01
5// clear && cargo run --example mut_var_sized --features err_02
6// clear && cargo run --example mut_var_sized --features err_03
7
8//=======
9#![allow(unused_variables)]
10
11
12
13
14//=======
15#[cfg(feature = "ok")]
16pub fn adjoin() {
17 // ANCHOR: feature-ok
18 // File: ./examples/mut_var_sized/string_ref.rs
19 // #[cfg(feature = "ok")]
20
21 let mut mut_instance = String::from("Hello");
22 mut_instance.push_str(", world");
23 println!("1. use mut_instance = {}", mut_instance);
24
25 let immut_ref = &mut_instance;
26 println!("1. use immut_ref = {}", immut_ref);
27
28 // The variable `mut_instance` borrowed here after move
29 println!("2. use mut_instance = {}", mut_instance);
30 println!("2. use immut_ref = {}", immut_ref);
31
32 // ANCHOR_END: feature-ok
33}
34
35
36
37
38//=======
39#[cfg(feature = "err_01")]
40pub fn adjoin() {
41 // ANCHOR: feature-error_01
42 // File: ./examples/mut_var_sized/string_ref.rs
43 // ANCHOR = "string_ref-error_01"
44 // error[E0382]: borrow of moved value: `mut_instance`
45
46 let mut mut_instance = String::from("hello");
47 mut_instance.push_str(", world!");
48 println!("1. use mut_instance = {}", mut_instance);
49
50 // The variable `mut_instance` begin to move here
51 let copy_mut_instance = mut_instance;
52 // The variable `mut_instance` moved here
53
54 // ERROR: The variable `mut_instance` borrowed here after move
55 println!("2. use mut_instance = {}", mut_instance); //ERROR
56
57 // ANCHOR_END: feature-error_01
58}
59
60
61
62
63//=======
64#[cfg(feature = "err_02")]
65pub fn adjoin() {
66 // ANCHOR: feature-error_02
67 // File: ./examples/mut_var_sized/string_ref.rs
68 // ANCHOR = "string_ref-error_02"
69 // error[E0502]: cannot borrow `mut_instance` as mutable because it is also borrowed as immutable
70
71 let mut mut_instance = String::from("Hello");
72 mut_instance.push_str(", world");
73 println!("1. use mut_instance = {}", mut_instance);
74
75 let immut_ref = &mut_instance;
76 println!("1. use immut_ref = {}", immut_ref);
77
78 // The variable `immut_ref` begin to move here
79 mut_instance.push_str("!");
80 // The variable `immut_ref` moved here
81 println!("2. use immut_ref = {}", immut_ref); //ERROR
82
83 // The variable `mut_instance` borrowed here after move
84 println!("2. use mut_instance = {}", mut_instance);
85
86 // ANCHOR_END: feature-error_02
87}
88
89
90
91
92//=======
93#[cfg(feature = "cp")]
94pub fn adjoin() {
95 // ANCHOR: feature-cp
96 // File: ./examples/mut_var_sized/string_ref.rs
97 // #[cfg(feature = "cp")]
98
99 let mut mut_instance = String::from("Hello");
100 mut_instance.push_str(", world");
101 println!("1. use mut_instance = {}", mut_instance);
102
103 // The variable `mut_instance` begin to move here
104 let mut_ref = &mut mut_instance;
105 // The variable `mut_instance` moved here
106 println!("1. use mut_ref = {}", mut_ref);
107 mut_ref.push_str("!");
108 println!("2. use mut_ref = {}", mut_ref);
109
110 mut_instance.make_ascii_uppercase();
111
112 println!("2. use mut_instance = {}", mut_instance);
113
114 // ANCHOR_END: feature-cp
115}
116
117
118
119
120//=======
121#[cfg(feature = "err_03")]
122pub fn adjoin() {
123 // ANCHOR: feature-error_03
124 // File: ./examples/mut_var_sized/string_ref.rs
125 // ANCHOR = "string_ref-error_03"
126 // error[E0499]: cannot borrow `mut_instance` as mutable more than once at a time
127
128 let mut mut_instance = String::from("Hello");
129 mut_instance.push_str(", world");
130 println!("1. use mut_instance = {}", mut_instance);
131
132 // The variable `mut_instance` begin to move here
133 let mut_ref = &mut mut_instance;
134 // The variable `mut_instance` moved here
135 println!("1. use mut_ref = {}", mut_ref);
136 mut_ref.push_str("!");
137 println!("2. use mut_ref = {}", mut_ref);
138
139 // The variable `mut_ref` begin to move here
140 mut_instance.make_ascii_uppercase();
141 // The variable `mut_ref` moved here
142
143 println!("3. use mut_ref = {}", mut_ref); // ERROR
144
145 // The variable `mut_instance` borrowed here after move
146 println!("2. use mut_instance = {}", mut_instance);
147
148 // ANCHOR_END: feature-error_03
149}
150
151
152
153
154//=======
155#[cfg(all(
156 not(feature = "ok"),
157 not(feature = "cp"),
158 not(feature = "err_01"),
159 not(feature = "err_02"),
160 not(feature = "err_03"),
161))]
162pub fn adjoin() {
163 use aide::hello;
164 hello();
165}