commit a37ee23bc2e90714fd5318bc07bef443bc0604fd Author: SowinskiBraeden Date: Fri Sep 11 13:50:31 2026 -0700 initial lab 01 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/lectures/01/Cargo.lock b/lectures/01/Cargo.lock new file mode 100644 index 0000000..da8b90f --- /dev/null +++ b/lectures/01/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "lec1" +version = "0.1.0" diff --git a/lectures/01/Cargo.toml b/lectures/01/Cargo.toml new file mode 100644 index 0000000..80f7777 --- /dev/null +++ b/lectures/01/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "lec1" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/lectures/01/src/arrays.rs b/lectures/01/src/arrays.rs new file mode 100644 index 0000000..7f24a99 --- /dev/null +++ b/lectures/01/src/arrays.rs @@ -0,0 +1,52 @@ +pub fn run() { + println!("ARRAYS"); + + // in rust arrays are values + // unlike in c where arrays are an address + let a = [3, 2, 7, 6, 8]; + println!("{a:?}"); // debug print + + let mut a = [3, 2, 7, 6, 8]; + a[0] = -3; + println!("{a:?}"); + + print_array5(a); + double_array5(&mut a); + print_array5_v2(&a); + + super::slices::double(&mut a); +} + +fn print_array5(a: [i32; 5]) { + // 0..5 is non inclusive + for i in 0..5 { + println!("{}", a[i]); + } + + /* this works also + x is i32 + for x in a { + println!("{x}") + } + */ +} + +fn print_array5_v2(a: &[i32; 5]) { + // x is &i32 + for x in a { + println!("{x}") + } +} + +fn double_array5(a: &mut [i32; 5]) { + /* + * this works + for i in 0..5 { + a[i] *= 2; + } + */ + + for x in a { + *x *= 2; + } +} diff --git a/lectures/01/src/main.rs b/lectures/01/src/main.rs new file mode 100644 index 0000000..c99743b --- /dev/null +++ b/lectures/01/src/main.rs @@ -0,0 +1,11 @@ +// import module numbers +mod arrays; +mod numbers; +mod slices; + +fn main() { + println!("Hello, world!"); + numbers::run(); + arrays::run(); + slices::run(); +} diff --git a/lectures/01/src/numbers.rs b/lectures/01/src/numbers.rs new file mode 100644 index 0000000..9f79a20 --- /dev/null +++ b/lectures/01/src/numbers.rs @@ -0,0 +1,63 @@ +// numbers an be imported, +// but functions need to be marked public with pub +pub fn run() { + println!("NUMBERS"); + + let x = 123; // numbers default to i32 + println!("{x}"); + // println!("{}", x + 100); // this panics if this line is enabled + println!("{}", x + 1i8); // x must be an i8 + + // variables are immutable by default, use mut to be mutable + let mut x = 2; + println!("{x}"); + x = 10; + println!("{x}"); + + // REFERENCES (borrowing) + let r = &x; // shared reference/immutable reference + // *r = 11; // not allowed - error + println!("{r}"); // println!() will always deref no matter how many refs, e.g. + println!("{}", &&&&&&x); + + let r2 = &x; + println!("{r} {r2}"); // scope of r & r2 is to here + + let s = &mut x; // mutable reference/exclusive reference + *s = 11; // allowed because s is mutable/exclusive + println!("{s}"); + + double(s); // or double(&mut x) + + // Ojects at any time can have either: + // many shared references + // OR + // exactly 1 mutable reference + // e.g. DB can have many readers but only 1 writer + // but not at the same time + + // therefore: + // println!("{r}"); will cause `let s = &mut x;` to error + // because both r & s are in scope at the same time + + /* + let mut x = 1; // can be modified + let r1 = &mut x; // can use r1 to modify x + let mut r2 = &x; // can modify r2 + let mut r3 = &mut x; // can modify r3 & x + */ + + // add float to int + // should default to f64 unless inferred as other, + // e.g. this will default to f32 since we add a f32 + let f = 3.14; + println!("{}", f + 1 as f32); // cast 1 to f32 + + let x = 10; + println!("{}", x + &1); +} + +// we need &mut to borrow original and mutate it +fn double(x: &mut i32) { + *x *= 2; +} diff --git a/lectures/01/src/slices.rs b/lectures/01/src/slices.rs new file mode 100644 index 0000000..bea871a --- /dev/null +++ b/lectures/01/src/slices.rs @@ -0,0 +1,44 @@ +mod utils; + +pub fn run() { + println!("SLICES"); + + let a = [3, 2, 7, 7, 8]; + let s1 = &a[..]; + let s2 = &a[1..3]; + let s3 = &a[2..]; + + println!("{s1:?} {s2:?} {s3:?}"); + + if let Some(r) = utils::find_even(&a) { + println!("first even integer: {r}"); + } else { + println!("no even integer"); + } + + let mut a = [3, 2, 7, 6, 8]; + utils::sextuple(&mut a); + println!("{a:?}"); + + fn divisible5(x: &i32) -> bool { + *x % 5 == 0 + } + + if let Some(r) = utils::find2(&a, divisible5) { + println!("found: {r}"); + } else { + println!("not found"); + } +} + +pub fn double(s: &mut [i32]) { + for x in s { + *x *= 2; + } +} + +pub fn triple(s: &mut [i32]) { + for x in s { + *x *= 3; + } +} diff --git a/lectures/01/src/slices/utils.rs b/lectures/01/src/slices/utils.rs new file mode 100644 index 0000000..ba8c552 --- /dev/null +++ b/lectures/01/src/slices/utils.rs @@ -0,0 +1,32 @@ +pub fn find_even(s: &[i32]) -> Option<&i32> { + for x in s { + if *x % 2 == 0 { + return Some(x); + } + } + None +} + +// can call function in ancestor even if not public +pub fn sextuple(s: &mut [i32]) { + super::triple(s); + super::double(s); +} + +pub fn find(s: &[i32], f: fn(&i32) -> bool) -> Option<&i32> { + for x in s { + if f(x) { + return Some(x); + } + } + None +} + +pub fn find2(s: &[T], f: fn(&T) -> bool) -> Option<&T> { + for x in s { + if f(x) { + return Some(x); + } + } + None +} diff --git a/useful.md b/useful.md new file mode 100644 index 0000000..5edacbd --- /dev/null +++ b/useful.md @@ -0,0 +1,34 @@ +Open book of rust +`rustup doc --book` + +Open std crate docs page +`rustup doc std` + +Open vec in std crate docs page +`rustup doc std::vec` + +Compile +`rustc .rs` + +Create package +`cargo new 01` + +Search cargo crates +`cargo search` + +Add crate +`cargo add {crate}` + +Build/run +`cargo build` +`cargo run` + +Setup +``` +cargo new lectures/01 --name lec01 +cargo new lectures/02 --name lec02 +cargo new lectures/03 --name lec03 + +cargo new labs/01 --name lab01 +cargo new labs/02 --name lab02 +```