ownership
This commit is contained in:
@@ -1,5 +1,34 @@
|
|||||||
fn main() {
|
fn main() {
|
||||||
let mut s = String::from("Hello");
|
let s1 = String::from("Hello");
|
||||||
s.push_str(", World!");
|
let s2 = s1; // s1 goes out of scope here
|
||||||
println!("{s}");
|
println!("{s2}, world!");
|
||||||
|
|
||||||
|
let mut s = String::from("hello");
|
||||||
|
s = String::from("world"); // original "hello" value drops here
|
||||||
|
println!("hello, {s}");
|
||||||
|
|
||||||
|
let s1 = String::from("hello");
|
||||||
|
let s2 = s1.clone(); // heap data gets copied here
|
||||||
|
println!("s1 = {s1}, s2 = {s2}");
|
||||||
|
|
||||||
|
let s3 = String::from("forsen");
|
||||||
|
copy_string_value(s3);
|
||||||
|
// println!("{s3}");
|
||||||
|
// will throw and erorr
|
||||||
|
|
||||||
|
let s4 = String::from("gachi");
|
||||||
|
let (s5, len) = calculate_length(s4);
|
||||||
|
//println!("{s4}");
|
||||||
|
// also will throw an error
|
||||||
|
println!("string: {s5}, length: {len}")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn copy_string_value(string: String) {
|
||||||
|
println!("{string}");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calculate_length(s: String) -> (String, usize) {
|
||||||
|
let length = s.len();
|
||||||
|
|
||||||
|
(s, length)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user