diff options
| author | Carlos Maiolino <[email protected]> | 2025-07-10 22:24:20 +0200 |
|---|---|---|
| committer | Carlos Maiolino <[email protected]> | 2025-07-10 22:24:20 +0200 |
| commit | 869e68986aa8f69af6e7842260a68d1e5c6f796f (patch) | |
| tree | 63b6b5ffc3d19414233d4629a533c0d9bf3cbf72 /rust/owner_ex.rs | |
| parent | 20834dcc57537cd95260a4a22f5d91a027adfd35 (diff) | |
Add a bunch of code
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'rust/owner_ex.rs')
| -rw-r--r-- | rust/owner_ex.rs | 35 |
1 files changed, 35 insertions, 0 deletions
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<i32> = Box::new(5); + + // y is also a pointer + let mut y: Box<i32> = Box::new(1); + + // Dereferencing Y address + *y = 4; + + assert_eq!(*x, 5); +} |
