mut_immut/
mut_string.rs

1// File: ./examples/mut_immut/mut_string.rs
2// clear && cargo run --example mut_immut --features ok -- mut_string | bat -l rs
3// clear && cargo run --example mut_immut --features okay -- mut_string | bat -l rs
4// clear && cargo run --example mut_immut --features okey -- mut_string | bat -l rs
5// clear && cargo run --example mut_immut --features err_01
6// clear && cargo run --example mut_immut --features err_02
7// clear && cargo run --example mut_immut --features err_03
8// clear && cargo run --example mut_immut --features err_04
9
10//=======
11#![allow(unused_variables)]
12
13
14
15
16//=======
17#[cfg(feature = "ok")]
18pub fn adjoin() {
19    // ANCHOR: feature-ok  AM BEST LĂ–SUNG
20    // File: ./examples/mut_immut/mut_string.rs
21    // #[cfg(feature = "ok")]
22
23    fn one(input: &str) -> String {  
24        let result = format!("{}{}", input, "?");
25        println!("two() input = {:p}", &input);
26        result
27    }
28    
29    fn two(input: &str) -> String {
30        let result = format!("{}{}", input, "?");
31        println!("two() input = {:p}", &input);
32        result
33    }
34
35    let mut instance = String::new();
36    println!("instance = {:p}", &instance);
37
38    instance.push_str("Hello");
39    println!("instance = {:p}", &instance);
40
41    instance = one(&instance);
42    println!("instance = {:p}", &instance);
43
44    instance = two(&instance);
45    println!("instance = {:p}", &instance);
46
47    println!("instance = {:?}", instance);
48
49    // ANCHOR_END: feature-ok
50}
51
52
53
54
55//=======
56#[cfg(feature = "cp")]
57pub fn adjoin() {
58    fn one(mut hello_from: String) -> String {
59        hello_from.push('!');
60        hello_from
61    }
62    
63    fn two(mut hello_from: String) -> String {
64        hello_from.push('?');
65        hello_from
66    }
67
68    let mut hello_from = String::new();
69    hello_from.push_str("Hello");
70    hello_from = one(hello_from);
71    hello_from = two(hello_from);
72    println!("hello from {:?}", hello_from);
73}
74
75
76
77//=======
78#[cfg(feature = "okay")]
79pub fn adjoin() {
80    // ANCHOR: feature-okay
81    // File: ./examples/mut_immut/mut_string.rs
82    // #[cfg(feature = "okay")]
83
84    fn one(input: &str) -> Box<String> {
85        let result = format!("{}{}", input, "?");
86        Box::new(result)
87    }
88
89    fn two(input: &str) -> Box<String> {
90        let result = format!("{}{}", input, "!");
91        Box::new(result)
92    }
93
94    let mut hello_from = String::new();
95    hello_from.push_str("Hello");
96
97    let mut mut_ref :&str = &hello_from;
98    hello_from = one(mut_ref).to_string();
99    mut_ref = &hello_from;
100    hello_from = two(mut_ref).to_string();
101    println!("hello from {:?}", hello_from);
102
103    let mut_ref :&str = &hello_from;
104    hello_from = one(mut_ref).to_string();
105    let mut_ref :&str = &hello_from;
106    hello_from = two(mut_ref).to_string();
107    println!("hello from {:?}", hello_from);
108
109    // ANCHOR_END: feature-okay
110}
111
112
113
114
115//=======
116#[cfg(feature = "okey")]
117pub fn adjoin() {
118    // ANCHOR: feature-okey
119    // File: ./examples/mut_immut/mut_string.rs
120    // #[cfg(feature = "okey")]
121
122    fn one(input: &mut String) -> Box<String> {
123        input.push('?');
124        Box::new(input.to_string())
125    }
126
127    fn two(input: &mut String) -> Box<String> {
128        input.push('!');
129        Box::new(input.to_string())
130    }    
131
132    let mut mut_instance = String::new();
133    mut_instance.push_str("Hello");
134    println!("1. mut_instance = {:?}", mut_instance);
135    
136    let mut mut_ref :&mut String = &mut mut_instance;
137    mut_instance = one(mut_ref).to_string();
138    println!("2. mut_instance = {:?}", mut_instance);
139    
140    mut_ref = &mut mut_instance;
141    mut_instance = two(mut_ref).to_string();
142    println!("3. mut_instance = {:?}", mut_instance);
143
144    // ANCHOR_END: feature-okey
145}
146
147
148
149
150//=======
151#[cfg(feature = "err_01")]
152pub fn adjoin() {
153    // ANCHOR: feature-error_01
154    // File: ./examples/mut_immut/mut_string.rs
155    // #[cfg(feature = "err_01")]
156
157    fn one(hello_from: &mut String) -> &String {
158        hello_from.push('!');
159        hello_from
160    }
161    
162    fn two(hello_from: &mut String) -> &String {
163        hello_from.push('?');
164        hello_from
165    }
166
167    let mut hello_from = String::new();
168    hello_from.push_str("Hello");
169    //let mut_ref :&mut String = &mut hello_from; //OK
170    //let mut_ref = &mut hello_from; //OK
171    let mut_ref :&String = &mut hello_from; //ERROR
172    one(mut_ref);
173    two(mut_ref);
174    println!("hello from {:?}", hello_from);
175
176    // ANCHOR_END: feature-error_01
177}
178
179
180
181
182//=======
183#[cfg(feature = "err_02")]
184pub fn adjoin() {
185    // ANCHOR: feature-error_02  OK!!!!
186    // File: ./examples/mut_immut/mut_string.rs
187    // #[cfg(feature = "err_02")]
188    
189    fn one(input: &str) -> Box<String> {
190        let mut buf = String::with_capacity(input.len());
191        buf.push_str(input);
192        buf.push('!');
193        buf.into()
194    }
195
196    fn two(input: &str) -> Box<String> {
197        let mut buf = String::with_capacity(input.len());
198        buf.push_str(input);
199        buf.push('!');
200        buf.into()
201    }
202
203
204    let mut hello_from = String::new();
205    hello_from.push_str("Hello");
206    let mut_ref :&str = &hello_from;
207    hello_from = one(mut_ref).to_string();
208    // let mut_ref :&str = &hello_from;  // OK
209    hello_from = two(mut_ref).to_string();
210    println!("hello from {:?}", hello_from);
211
212    // ANCHOR_END: feature-error_02
213}
214
215
216
217
218//=======
219#[cfg(feature = "err_03")]
220pub fn adjoin() {
221    // ANCHOR: feature-error_03
222    // File: ./examples/mut_immut/mut_string.rs
223    // #[cfg(feature = "err_03")]
224    
225    use std::borrow::Cow;
226
227    fn one<'a>(input: &'a str) -> Cow<'a, str> {
228        let mut buf = String::with_capacity(input.len());
229        buf.push_str(input);
230        buf.push('!');
231        buf.into()
232    }
233
234    fn two<'a>(input: &'a str) -> Cow<'a, str> {
235        let mut buf = String::with_capacity(input.len());
236        buf.push_str(input);
237        buf.push('!');
238        buf.into()
239    }    
240
241    let mut hello_from = String::new();
242    hello_from.push_str("Hello");
243
244    let mut_ref :&str = &hello_from;
245    let fn_one :Cow<'_, str> = one(mut_ref);
246    hello_from = fn_one.to_string();
247
248    // let mut_ref :&str = &hello_from; //OK
249    hello_from = two(mut_ref).to_string();
250    
251    println!("hello from {:?}", hello_from);
252
253    // ANCHOR_END: feature-error_03
254}
255
256
257
258
259//=======
260#[cfg(feature = "err_04")]
261pub fn adjoin() {
262    // ANCHOR: feature-error_04
263    // File: ./examples/mut_immut/mut_string.rs
264    // #[cfg(feature = "err_04")]
265    
266    fn one(input: &str) -> Box<String> {
267        let result = [input, "!"].join("");
268        println!("one() input = {:p}", &input);
269        let result = [input, "!"].concat();
270        println!("one() input = {:p}", &input);
271        Box::new(result)
272    }
273
274    fn two(input: &str) -> Box<String> {
275        let result = [input, "!"].join("");
276        println!("one() input = {:p}", &input);
277        let result = [input, "!"].concat();
278        println!("one() input = {:p}", &input);
279        Box::new(result)
280    }    
281
282    let mut hello_from = String::new();
283    hello_from.push_str("Hello");
284    //let mut_ref :&mut str = &hello_from; ERROR
285    
286    //let mut_ref :&str = &mut hello_from; // OK
287    let mut_ref :&str = &hello_from; // OK
288
289    //let mut_ref :&mut str = &mut hello_from; // OK
290    
291    //let mut mut_ref :&str = &hello_from; // OK
292    //let mut mut_ref :&str = &mut hello_from;  // OK, warning
293    //let mut mut_ref :&mut str = &mut hello_from;  // OK, warning
294
295    hello_from = one(mut_ref).to_string();
296    
297    // let mut_ref :&str = &hello_from; // OK
298    hello_from = two(mut_ref).to_string();
299
300    println!("hello from {:?}", hello_from);
301
302    // ANCHOR_END: feature-error_04
303}
304
305
306
307//=======
308#[cfg(feature = "err_05")]
309pub fn adjoin() {
310    // ANCHOR: feature-error_05
311    // File: ./examples/mut_immut/mut_string.rs
312    // #[cfg(feature = "err_05")]
313
314    fn one(hello_from: &mut String) -> &String {
315        hello_from.push('!');
316        hello_from
317    }
318    
319    fn two(hello_from: &mut String) -> &String {
320        hello_from.push('?');
321        hello_from
322    }
323
324    let mut hello_from = String::new();
325    hello_from.push_str("Hello");
326    //let mut_ref :&mut String = &mut hello_from; // OK
327    let mut_ref :&String = &mut hello_from;  // ERROR
328    one(mut_ref);
329    two(mut_ref);
330    println!("hello from {:?}", hello_from);
331
332    // ANCHOR_END: feature-error_05
333}
334
335
336
337
338//=======
339#[cfg(feature = "err_06")]
340pub fn adjoin() {
341    // ANCHOR: feature-error_06
342    // File: ./examples/mut_immut/mut_string.rs
343    // #[cfg(feature = "err_06")]
344
345    // fn one(mut hello_from: String) -> String { // OK
346    fn one(hello_from: String) -> String {
347        hello_from.push('!');
348        hello_from
349    }
350    
351    // fn two(mut hello_from: String) -> String { // OK
352    fn two(hello_from: String) -> String {
353        hello_from.push('?');
354        hello_from
355    }
356
357    let mut hello_from = String::new();
358    hello_from.push_str("Hello");
359    hello_from = one(hello_from);
360    hello_from = two(hello_from);
361    println!("hello from {:?}", hello_from);
362
363    // ANCHOR_END: feature-error_06
364}
365
366
367
368
369//=======
370#[cfg(all(
371    not(feature = "ok"),
372    not(feature = "cp"),
373    not(feature = "okay"),
374    not(feature = "okey"),
375    not(feature = "err_01"),
376    not(feature = "err_02"),
377    not(feature = "err_03"),
378    not(feature = "err_04"),
379    not(feature = "err_05"),
380    not(feature = "err_06"),
381))]
382pub fn adjoin() {
383    use aide::*;
384    hello();
385}
386
387//
388// https://stackoverflow.com/questions/42248444/return-str-instead-of-stdborrowcow-str
389// https://hermanradtke.com/2015/05/29/creating-a-rust-function-that-returns-string-or-str.html
390// https://hermanradtke.com/2015/05/03/string-vs-str-in-rust-functions.html
391
392// https://stackoverflow.com/questions/30154541/how-do-i-concatenate-strings
393// https://users.rust-lang.org/t/best-way-to-do-string-concatenation-in-2019-status-quo/24004/4
394// https://users.rust-lang.org/t/fast-string-concatenation/4425/2
395// http://dnsh.io/music/2016/10/06/string-concatenation-in-rust-is-not-tivial/
396