From 1d8a7faddf668821ffb61596e5f1d8d090bd31b1 Mon Sep 17 00:00:00 2001 From: Sil Klaasboer Date: Thu, 18 Dec 2025 13:52:08 +0100 Subject: [PATCH] add ownership and borrowing testing --- ownership/Cargo.lock | 7 +++++++ ownership/Cargo.toml | 6 ++++++ ownership/src/main.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 ownership/Cargo.lock create mode 100644 ownership/Cargo.toml create mode 100644 ownership/src/main.rs diff --git a/ownership/Cargo.lock b/ownership/Cargo.lock new file mode 100644 index 0000000..8c5b86e --- /dev/null +++ b/ownership/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "ownership" +version = "0.1.0" diff --git a/ownership/Cargo.toml b/ownership/Cargo.toml new file mode 100644 index 0000000..a4b0049 --- /dev/null +++ b/ownership/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/ownership/src/main.rs b/ownership/src/main.rs new file mode 100644 index 0000000..a6faa6b --- /dev/null +++ b/ownership/src/main.rs @@ -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); + }