Files
learn-rust/ownership/src/main.rs
2025-07-01 15:06:05 +03:00

35 lines
855 B
Rust

fn main() {
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)
}