lecture 02

This commit is contained in:
SowinskiBraeden committed 2026-09-18 15:35:00 -07:00
1 parent a37ee23bc2
commit 50bee77117
11 files changed
+300

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
mod strings;
mod structs;
mod tuples;
mod vectors;
fn main() {
println!("Hello, world!");
tuples::run();
vectors::run();
strings::run();
structs::run();
}
+39
View File
@@ -0,0 +1,39 @@
pub fn run() {
println!("STRINGS");
let s1 = "hello"; // type &str
let s2 = String::from("world"); // type String
print_str(s1);
// print_string(s1); // ERROR
// print_string_ref(s1); // ERROR
print_str(&s2); // also accepts &String (Deref trait)
print_string_ref(&s2);
print_string(s2); // moves s2
let s = "नमस्ते";
println!("{}", s.len()); // byte count -> 18
// println!("{}", s[0]); // can't index string
println!("{:?}", &s[0..3]); // may panic if not at char boundary
for c in s.chars() {
println!("{c}");
}
println!("{}", s.chars().count());
println!("{:?}", s.as_bytes());
}
// which version of string function should be used?
// CONCLUSION: function that takes &str is most flexible
fn print_str(s: &str) {
println!("{s}");
}
fn print_string(s: String) {
println!("{s}");
}
fn print_string_ref(s: &String) {
println!("{s}");
}
+66
View File
@@ -0,0 +1,66 @@
use std::fmt;
#[derive(Debug, Copy, Clone)] // derive the implementatoin of the Debug trait (like an interface)
struct Point(f64, f64); // tuple struct
#[derive(Debug, Copy, Clone)]
pub struct Circle {
center: Point,
radius: f64,
}
impl fmt::Display for Point {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {})", self.0, self.1)
}
}
impl Circle {
// or you can specify -> Self
pub fn new(center: Point, radius: f64) -> Circle {
// Or you can specify self instead of Circle {
Circle { center, radius } // since names are the same, we dont need key: value pair
}
pub fn area(&self) -> f64 {
std::f64::consts::PI * self.radius * self.radius
}
pub fn scale(&mut self, factor: f64) {
self.radius *= factor;
}
}
impl fmt::Display for Circle {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Circle: {} {}", self.center, self.radius)
}
}
pub fn run() {
println!("STRUCTS");
let p = Point(1.0, 2.0);
println!("{} {}", p.0, p.1);
let Point(x, y) = p;
println!("{x} {y}");
let c = Circle {
center: p,
radius: 1.0,
};
println!("{c:?}");
let mut c = Circle::new(Point(0.0, 0.0), 1.0);
println!("{}", c.area()); // area will automatically take reference
println!("{}", (&c).area()); // or you can specify manually
c.scale(2.0);
println!("{c:?}");
println!("{}", c.area());
// after implementing Display trait
println!("{c}");
let _s = format!("{c}"); // format uses Display trait, to convert to string
}
+15
View File
@@ -0,0 +1,15 @@
pub fn run() {
println!("TUPLES");
let t = (1, 3.14);
println!("{} {}", t.0, t.1);
println!("{t:?}"); // debug print
let (x, y) = t; // pattern matching
println!("{x} {y}");
println!("{:?}", swap(&t));
}
fn swap(t: &(i32, f64)) -> (f64, i32) {
(t.1, t.0)
}
+47
View File
@@ -0,0 +1,47 @@
pub fn run() {
println!("VECTORS");
let v = vec![3, 2, 7, 6, 8]; // macro for creating vector with known type
println!("{v:?}");
for i in 0..v.len() {
println!("{}", v[i]);
}
// x has typ &i32
// for x in &v {
// println!("{x}");
// }
// x hsa type i32
// for x in v { // this moves v
// println!("{x}");
// }
// println!("{v:?}"); // ERROR v is already moved
let mut v = Vec::new();
for i in 0..10 {
v.push(i);
}
double(&mut v);
println!("{v:?}");
triple(&mut v); // you can pass vectors as slices also
println!("{v:?}");
triple(&mut v[1..4]); // can create sub slices
println!("{v:?}");
}
fn double(v: &mut Vec<i32>) {
for x in v {
*x *= 2
}
}
fn triple(s: &mut [i32]) {
for x in s {
*x *= 3
}
}