From 9c19d631217172e32f11ac58a54be92dfdad66af Mon Sep 17 00:00:00 2001 From: Amoelle Date: Tue, 1 Jul 2025 15:06:05 +0300 Subject: [PATCH] ownership --- ownership/src/main.rs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/ownership/src/main.rs b/ownership/src/main.rs index 5a2450d..854d9ce 100644 --- a/ownership/src/main.rs +++ b/ownership/src/main.rs @@ -1,5 +1,34 @@ fn main() { - let mut s = String::from("Hello"); - s.push_str(", World!"); - println!("{s}"); + let s1 = String::from("Hello"); + let s2 = s1; // s1 goes out of scope here + 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) }