mut_fn/
base_str.rs

1// File: ./examples/mut_fn/base_str.rs
2// clear && cargo run --example mut_fn --features okey -- base_str | bat -l rs
3// clear && cargo run --example mut_fn --features err_01
4// clear && cargo run --example mut_fn --features err_02
5
6//=======
7#![allow(unused_variables)]
8
9
10
11//=======
12#[cfg(feature = "ok")]
13pub fn adjoin() {
14    // ANCHOR: feature-ok
15    // File: ./examples/mut_fn/base_str.rs
16    // #[cfg(feature = "ok")]
17    // FIXED
18
19    fn one<'de>(x: &'de str, y: &'de str) -> &'de str {
20        println!("x = {:p}", x);
21        println!("y = {:p}", y);
22        if x.is_empty() {
23            x
24        } else {
25            y
26        }
27    }
28
29    let first_str = "";
30    let second_str = "World";
31    println!("first_str = {:p}", first_str);
32    println!("second_str = {:p}", second_str);
33    let result = one(first_str, second_str);
34
35    println!();
36    println!("first_str = {:p}", first_str);
37    println!("second_str = {:p}", second_str);
38    println!("result = {:p}", result);
39    println!("result = {}", result);
40
41    // ANCHOR_END: feature-ok
42}
43
44
45
46//=======
47#[cfg(feature = "cp")]
48pub fn adjoin() {
49    // ANCHOR: feature-cp
50    // File: ./examples/mut_fn/base_str.rs
51    // #[cfg(feature = "cp")]
52    // 
53
54    fn one(x: &str, y: &str) {
55        println!("x = {:p}", x);
56        println!("y = {:p}", y);
57    }
58    
59    let hello = "Hello";
60    let world = "World";
61    println!("hello = {:p}", hello);
62    println!("world = {:p}", world);
63    one(hello, world);
64    
65    // ANCHOR_END: feature-cp
66}
67
68
69
70
71//=======
72#[cfg(feature = "okay")]
73pub fn adjoin() {
74    // ANCHOR: feature-okay
75    // File: ./examples/mut_fn/base_str.rs
76    // #[cfg(feature = "okay")]
77    // compare to #[cfg(feature = "err_08")]
78
79    fn one<'cn>(x: &'cn str, y: &str) -> &'cn str {
80        x
81    }
82
83    let first_str = "Hello";
84    let second_str = "World";
85    let result = one(first_str, second_str);
86    println!("result = {}", result);
87
88    // ANCHOR_END: feature-okay
89}
90
91
92
93//=======
94#[cfg(feature = "okey")]
95pub fn adjoin() {
96    // ANCHOR: feature-okey
97    // File: ./examples/mut_fn/base_str.rs
98    // #[cfg(feature = "okey")]
99    // FIXED
100
101    fn one(input: &str) {
102        println!("one() input = {:p}", input);
103
104        let ret_input = input.to_ascii_uppercase();
105        println!("one() ret_input = {:p}", &ret_input);
106        println!("one() ret_input = {}", ret_input);
107        
108        println!("one() input = {:p}", input);
109        println!("one() input = {}", input);
110    }
111 
112    let mut instance = String::new();
113    instance.push_str("Hello");
114    println!("after instance change = {:p}", &instance);
115    println!();
116
117    let immut_ref :&str = &instance;
118    println!("before call one instance = {:p}", immut_ref);
119    one(immut_ref);
120    println!("after call one instance = {:p}", immut_ref);
121
122    println!("instance = {}", instance);
123
124    // ANCHOR_END: feature-okey
125}
126
127
128
129
130//=======
131#[cfg(feature = "err_07")]
132pub fn adjoin() {
133    // ANCHOR: feature-error_07
134    // File: ./examples/mut_fn/base_str.rs
135    // #[cfg(feature = "err_07")]
136    // error[E0106]: missing lifetime specifier
137    // FIXED
138
139    fn one(x: &str, y: &str) -> &str {
140        x
141    }
142
143    let first_str = "Hello";
144    let second_str = "World";
145    let result = one(first_str, second_str);
146    println!("result = {}", result);
147
148
149    // ANCHOR_END: feature-error_07
150}
151
152
153
154
155//=======
156#[cfg(feature = "err_08")]
157pub fn adjoin() {
158    // ANCHOR: feature-error_02
159    // File: ./examples/mut_fn/base_str.rs
160    // #[cfg(feature = "err_08")]
161    // 
162    // 
163
164    fn one<'cn>(x: &'cn str, y: &str) -> &'cn str {
165        y
166    }
167
168    let first_str = "Hello";
169    let second_str = "World";
170    let result = one(first_str, second_str);
171    println!("result = {}", result);
172
173
174    // ANCHOR_END: feature-error_02
175}
176
177
178
179
180//=======
181#[cfg(feature = "err_09")]
182pub fn adjoin() {
183    // ANCHOR: feature-error_03
184    // File: ./examples/mut_fn/base_str.rs
185    // #[cfg(feature = "err_09")]
186    // error[E0106]: missing lifetime specifier
187    // 
188
189    fn one(x: &str, y: &String) -> &str {
190        x
191    }
192
193    let first_str = "Hello";
194    let second_str = "World".to_string();
195    let result = one(first_str, second_str);
196    println!("result = {}", result);
197
198
199    // ANCHOR_END: feature-error_03
200}
201
202
203
204//=======
205#[cfg(feature = "err_10")]
206pub fn adjoin() {
207    // ANCHOR: feature-error_04
208    // File: ./examples/mut_fn/base_str.rs
209    // #[cfg(feature = "err_10")]
210    // error[E0106]: missing lifetime specifier
211    // 
212
213    //fn one() -> &'static str { //OK
214    fn one() -> &'de str {
215        "Hello"
216    }
217
218    let result = one();
219    println!("result = {}", result);
220
221    // ANCHOR_END: feature-error_04
222}
223
224
225
226//=======
227#[cfg(all(
228    not(feature = "ok"),
229    not(feature = "cp"),
230    not(feature = "okay"),
231    not(feature = "okey"),
232    not(feature = "err_07"),
233    not(feature = "err_08"),
234    not(feature = "err_09"),
235    not(feature = "err_10"),
236))]
237pub fn adjoin() {
238    use aide::*;
239    hello();
240}
241
242// https://play.rust-lang.org/?gist=cae31659a0caa396c2702039b5e16964&version=stable