From 1f1ea6de94e6668a8ba2cab3764e979b9265b54b Mon Sep 17 00:00:00 2001 From: Luke Tidd Date: Fri, 16 Dec 2022 16:26:46 -0500 Subject: [PATCH] ownership, references --- ch03_practice/Cargo.toml | 8 +++++ ch03_practice/src/main.rs | 40 ++++++++++++++++++++++ notes | 72 +++++++++++++++++++++++++++++++++++++++ ownership/Cargo.toml | 8 +++++ ownership/src/main.rs | 43 +++++++++++++++++++++++ 5 files changed, 171 insertions(+) create mode 100644 ch03_practice/Cargo.toml create mode 100644 ch03_practice/src/main.rs create mode 100644 ownership/Cargo.toml create mode 100644 ownership/src/main.rs diff --git a/ch03_practice/Cargo.toml b/ch03_practice/Cargo.toml new file mode 100644 index 0000000..509298c --- /dev/null +++ b/ch03_practice/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ch03_practice" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/ch03_practice/src/main.rs b/ch03_practice/src/main.rs new file mode 100644 index 0000000..304475c --- /dev/null +++ b/ch03_practice/src/main.rs @@ -0,0 +1,40 @@ +fn f_to_c(f: f64) -> f64 { + (f - 32.0) * (5.0 / 9.0) +} + +fn c_to_f(c: f64) -> f64 { + c * (9.0 / 5.0) + 32.0 +} + +fn main() { + + // f to c / c to f + let f1 = 0.0; + let f2 = 32.0; + let f3 = 80.0; + let f4 = 212.0; + let c1 = f_to_c(f1); + let c2 = f_to_c(f2); + let c3 = f_to_c(f3); + let c4 = f_to_c(f4); + + println!("{f1}f {c1}c"); + println!("{f2}f {c2}c"); + println!("{f3}f {c3}c"); + println!("{f4}f {c4}c"); + + let f5 = c_to_f(100.0); + println!("100c = {f5}f"); + // fib + // + let mut x1 = 0; + let mut x2 = 1; + let mut temp = 0; + while x1 < 2000 { + println!("x: {x1}"); + temp = x2; + x2 = x1 + x2; + x1 = temp; + } +} + diff --git a/notes b/notes index c724432..af8befd 100644 --- a/notes +++ b/notes @@ -138,3 +138,75 @@ syntax: for x in y {} (start..end) is a Range (1..4).rev() is a thing + + +https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html?search= +ownership + +stack - lifo in order memory storage, stack of PLATES + pushing / popping + all data stored here must have a FIXED size + +heap - rando shit goes here + allocating / deallocating + pointer to data structure goes on the stack, the actual data goes on the heap + +each value in Rust has an _owner_ +there can be only one owner at a time +when the owner goes out of scope, the value goes out of scope + +a string has 3 parts + +name | val +----------- +ptr | ------> index | val +len | 5 | ------------- +cap | 5 | | 0 | 'h' | +----------- | 1 | 'e' | + + +string2 = string1; +create a new string data structure +have the pointer point to the same data as string1 + +invalid: + +s2 = s1; +println!("{s1}"); + +there can't be two pointers to the same value! + +shallow copy is a move because the first pointer is invalidated + +clone is "deep copy" + +s2 = s1.clone(); + +stack based vars don't need to clone, they `copy`, it's automatic + +drop is called to return memory from the heap when it's owner goes out of scope + +any type that implements the copy trait will auto copy. +a type can not implement copy and drop... + +tuples will copy if all of their types support it, it will not if any one of them does not + +a function can take an immutable heap mounted variable and "recast" it is mutable + +https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html +references + +rust has a method to let a function use a variable without moving / reassigning +ownership: references + +& is a reference +* is a dereference + +pass a reference to avoid _moving_ the value +references are immutable by default + +&mut var // makes a mutable reference + +there can be one or multiple r/o references or only one mutable reference at a time + + diff --git a/ownership/Cargo.toml b/ownership/Cargo.toml new file mode 100644 index 0000000..302f515 --- /dev/null +++ b/ownership/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/ownership/src/main.rs b/ownership/src/main.rs new file mode 100644 index 0000000..9c397e5 --- /dev/null +++ b/ownership/src/main.rs @@ -0,0 +1,43 @@ +fn main() { + let mut s = String::from("farts"); + let s2 = String::from("farts"); + s.push_str(" and blarts"); + println!("Hello, {s}!"); + + s = String::from("hello"); + takes_ownership(s.clone()); + takes_ownership(s.clone()); + let r1 = &mut s; + takes_ref(r1); + let r2 = &mut s; + takes_ref(r2); + //takes_ref(r1); + let x = 5; + makes_copy(x); + let s3 = take_and_give_back(s); + println!("cha cha cha {}", s3); + let s4 = take_and_give_back(String::from("farts")); + let s5 = take_and_give_back(s2); + + + println!("cha cha cha {} {}", s4, s5); + +} + +fn take_and_give_back(mut some_string: String) -> String { + some_string.push_str("caflobble"); + some_string +} + +fn takes_ref(some_string: &mut String) { + some_string.push_str("sdf"); + println!("{}", some_string); +} + +fn takes_ownership(some_string: String) { + println!("{}", some_string); +} + +fn makes_copy(some_int: u8) { + println!("some int: {}", some_int); +}