add ownership and borrowing testing

This commit is contained in:
Sil Klaasboer
2025-12-18 13:52:08 +01:00
parent 2069c2d469
commit 1d8a7faddf
3 changed files with 47 additions and 0 deletions

34
ownership/src/main.rs Normal file
View File

@@ -0,0 +1,34 @@
fn main() {
let a ="Hello".to_string();
let b = a;
//println!("{}",a); This will error because a no longer owns the string
println!("{}",b);
//simple types like number characters and booleans are copied not moved
let c = 5;
let d = c;
println!("{}",c);
println!("{}",d);
// when ownership does not need to be moved clone can be used.
let e ="Test".to_string();
let f = e.clone();
println!("{}",e);
println!("{}",f);
// references in cpp is called borrowing in rust
let g = String::from("Reference test");
let h = &g;
println!("{}", g);
println!("{}", h);
// mutable references
let mut i = String::from("Mutable Reference test");
let j = &mut i;
j.push('!');
println!("{}", j);
}