mut_immut/
ref_str.rs

1// File: ./examples/mut_immut/ref_str.rs
2// clear && cargo run --example mut_immut --features ok -- ref_str | bat -l rs
3// clear && cargo run --example mut_immut --features cp -- ref_str | bat -l rs
4// clear && cargo run --example mut_immut --features okay -- ref_str | bat -l rs
5// clear && cargo run --example mut_immut --features okey -- ref_str | bat -l rs
6// clear && cargo run --example mut_immut --features err_01
7// clear && cargo run --example mut_immut --features err_02
8// clear && cargo run --example mut_immut --features err_03
9
10//=======
11#![allow(unused_variables)]
12
13
14
15//=======
16#[cfg(feature = "ok")]
17pub fn adjoin() {
18    // ANCHOR: feature-ok  AM BEST LĂ–SUNG
19    // File: ./examples/mut_immut/ref_str.rs
20    // #[cfg(feature = "ok")]
21
22    fn one(input: &str) -> String {  
23        let result = format!("{}{}", input, ", world");
24        println!("one() input = {:p}", input);
25        result
26    }
27    
28    fn two(input: &str) -> String {
29        let result = format!("{}{}", input, "!");
30        println!("two() input = {:p}", input);
31        result
32    }
33
34    let mut instance = String::new();
35    println!("instance = {:p}", &instance);
36    instance.push_str("Hello");
37    println!();
38
39    let mut mut_immut_ref :&str = &instance;
40    println!("before call one() mut_immut_ref = {:p}", mut_immut_ref);
41    let tmp_instance = one(mut_immut_ref);
42    println!("after call one() mut_immut_ref = {:p}", mut_immut_ref);
43    instance = tmp_instance;
44    println!("instance = {:?}", instance);
45    println!();
46
47    mut_immut_ref = &instance;
48    println!("mut_immut_ref = {:p}", mut_immut_ref);
49    instance = two(mut_immut_ref);
50    println!("instance = {:?}", instance);
51    println!();
52
53    println!("instance = {:p}", &instance);
54
55    // ANCHOR_END: feature-ok
56}
57
58
59
60//=======
61#[cfg(feature = "cp")]
62pub fn adjoin() {
63    // ANCHOR: feature-cp
64    // File: ./examples/mut_immut/ref_str.rs
65    // #[cfg(feature = "cp")]
66
67    use std::borrow::Cow;
68
69    fn one<'a>(input: &'a str) -> Cow<'a, str> {
70        let mut buf = String::with_capacity(input.len());
71        buf.push_str(input);
72        buf.push_str(", world");
73        buf.into()
74    }
75
76    fn two<'a>(input: &'a str) -> Cow<'a, str> {
77        let mut buf = String::with_capacity(input.len());
78        buf.push_str(input);
79        buf.push('!');
80        buf.into()
81    }    
82
83    let mut hello_from = String::new();
84    hello_from.push_str("Hello");
85
86    let mut_ref :&str = &hello_from;
87    let fn_one :Cow<'_, str> = one(mut_ref);
88    hello_from = fn_one.to_string();
89
90    let mut_ref :&str = &hello_from;
91    hello_from = two(mut_ref).to_string();
92    
93    println!("hello from {:?}", hello_from);
94
95    // ANCHOR_END: feature-cp
96}
97
98
99
100//=======
101#[cfg(feature = "okay")]
102pub fn adjoin() {
103    // ANCHOR: feature-okay
104    // File: ./examples/mut_immut/ref_str.rs
105    // #[cfg(feature = "okay")]
106
107    fn one(input: &str) -> Box<String> {
108        let result = format!("{}{}", input, ", world");
109        Box::new(result)
110    }
111
112    fn two(input: &str) -> Box<String> {
113        let result = format!("{}{}", input, "!");
114        Box::new(result)
115    }
116
117    let mut hello_from = String::new();
118    hello_from.push_str("Hello");
119
120    let mut_ref :&str = &hello_from;
121    hello_from = one(mut_ref).to_string();
122    let mut_ref :&str = &hello_from;
123    hello_from = two(mut_ref).to_string();
124    println!("hello from {:?}", hello_from);
125
126    let mut mut_ref :&str = &hello_from;
127    hello_from = one(mut_ref).to_string();
128    mut_ref = &hello_from;
129    hello_from = two(mut_ref).to_string();
130    println!("hello from {:?}", hello_from);
131
132    // ANCHOR_END: feature-okay
133}
134
135
136
137
138//=======
139#[cfg(feature = "okey")]
140pub fn adjoin() {
141    // ANCHOR: feature-okey
142    // File: ./examples/mut_immut/ref_str.rs
143    // #[cfg(feature = "okey")]
144
145
146    // ANCHOR_END: feature-okey
147}
148
149
150
151
152//=======
153#[cfg(feature = "err_01")]
154pub fn adjoin() {
155    // ANCHOR: feature-error_01
156    // File: ./examples/mut_immut/ref_str.rs
157    // #[cfg(feature = "err_01")]
158
159    fn one(input: &str) -> Box<String> {
160        let result = format!("{}{}", input, ", world");
161        Box::new(result)
162    }
163
164    fn two(input: &str) -> Box<String> {
165        let result = format!("{}{}", input, "!");
166        Box::new(result)
167    }
168
169    let mut hello_from = String::new();
170    hello_from.push_str("Hello");
171
172    let mut_ref :&str = &hello_from;
173    hello_from = one(mut_ref).to_string();
174    // mut_ref = &hello_from;  // ERROR
175    hello_from = two(mut_ref).to_string();
176    println!("hello from {:?}", hello_from);
177
178    // ANCHOR_END: feature-error_01
179}
180
181
182
183//=======
184#[cfg(feature = "err_02")]
185pub fn adjoin() {
186    // ANCHOR: feature-error_02  OK!!!!
187    // File: ./examples/mut_immut/ref_str.rs
188    // #[cfg(feature = "err_02")]
189    
190
191
192    // ANCHOR_END: feature-error_02
193}
194
195
196
197
198//=======
199#[cfg(feature = "err_03")]
200pub fn adjoin() {
201    // ANCHOR: feature-error_03
202    // File: ./examples/mut_immut/ref_str.rs
203    // #[cfg(feature = "err_03")]
204    
205    fn manipulate(input: &mut String) {                                                                                                                                                                                                          
206        input.push('!');
207      }
208 
209    let mut instance = String::new();
210    instance.push_str("Hello");
211    let mut_ref = &mut instance;
212    manipulate(mut_ref);          
213    println!("{}", instance);    
214
215    // ANCHOR_END: feature-error_03
216}
217
218
219
220//=======
221#[cfg(all(
222    not(feature = "ok"),
223    not(feature = "cp"),
224    not(feature = "okay"),
225    not(feature = "okey"),
226    not(feature = "err_01"),
227    not(feature = "err_02"),
228    not(feature = "err_03"),
229))]
230pub fn adjoin() {
231    use aide::*;
232    hello();
233}
234
235//
236// https://stackoverflow.com/questions/42248444/return-str-instead-of-stdborrowcow-str
237// https://hermanradtke.com/2015/05/29/creating-a-rust-function-that-returns-string-or-str.html
238// https://hermanradtke.com/2015/05/03/string-vs-str-in-rust-functions.html
239
240// https://stackoverflow.com/questions/30154541/how-do-i-concatenate-strings
241// https://users.rust-lang.org/t/best-way-to-do-string-concatenation-in-2019-status-quo/24004/4
242// https://users.rust-lang.org/t/fast-string-concatenation/4425/2
243// http://dnsh.io/music/2016/10/06/string-concatenation-in-rust-is-not-tivial/
244