From 869e68986aa8f69af6e7842260a68d1e5c6f796f Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Thu, 10 Jul 2025 22:24:20 +0200 Subject: Add a bunch of code Signed-off-by: Carlos Maiolino --- rust/owner_ex.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 rust/owner_ex.rs (limited to 'rust/owner_ex.rs') diff --git a/rust/owner_ex.rs b/rust/owner_ex.rs new file mode 100644 index 0000000..f76a783 --- /dev/null +++ b/rust/owner_ex.rs @@ -0,0 +1,35 @@ +fn main() { + + // This is interesting... + // &str annotate we are using a string literal, not creating + // a string at run time. + // It means the size of the tuple is known at compilation time. + let x: (i32, i32, (), &str) = (1, 2, (), "hello"); + + // Because we know the tuple size at compilation time, (and likely + // because it's allocated on the stack, we can simply copy it here. + let y: (i32, i32, (), &str) = x; + + println!("{:?}, {:?}", x, y); + + // s oritinally immutable + let s: String = String::from("hello, "); + + // Transferring ownership can be done to a mutable variable + let mut s1 = s; + + s1.push_str("world"); + + println!("{}", s1); + + // x now is a pointer to the heap memory + let x: Box = Box::new(5); + + // y is also a pointer + let mut y: Box = Box::new(1); + + // Dereferencing Y address + *y = 4; + + assert_eq!(*x, 5); +} -- cgit v1.2.3