mut_var_sized/
string_refs.rs

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