initial lab 01

This commit is contained in:
SowinskiBraeden committed 2026-09-11 13:50:31 -07:00
commit a37ee23bc2
9 files changed
+250

No files matched your search

+1
View File
@@ -0,0 +1 @@
target/
+7
View File
@@ -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"
+6
View File
@@ -0,0 +1,6 @@
[package]
name = "lec1"
version = "0.1.0"
edition = "2024"
[dependencies]
+52
View File
@@ -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;
}
}
+11
View File
@@ -0,0 +1,11 @@
// import module numbers
mod arrays;
mod numbers;
mod slices;
fn main() {
println!("Hello, world!");
numbers::run();
arrays::run();
slices::run();
}
+63
View File
@@ -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;
}
+44
View File
@@ -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;
}
}
+32
View File
@@ -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<T>(s: &[T], f: fn(&T) -> bool) -> Option<&T> {
for x in s {
if f(x) {
return Some(x);
}
}
None
}
+34
View File
@@ -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 <filename>.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
```