ownership, references

This commit is contained in:
LuKe Tidd 2022-12-16 16:26:46 -05:00
parent 9a7a9558b5
commit 1f1ea6de94
Signed by: luke
GPG Key ID: 75D6600BEF4E8E8F
5 changed files with 171 additions and 0 deletions

8
ch03_practice/Cargo.toml Normal file
View File

@ -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]

40
ch03_practice/src/main.rs Normal file
View File

@ -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;
}
}

72
notes
View File

@ -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

8
ownership/Cargo.toml Normal file
View File

@ -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]

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

@ -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);
}