Last active
October 8, 2025 14:40
-
-
Save mmj-the-fighter/6915118a843314c9c040a78f82d2ccaa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //1. helloworld | |
| fn nod() { | |
| println!("hello, world!"); | |
| } | |
| //2. printing | |
| fn printing() { | |
| let num: i32 = 5; | |
| let pi: f32 = 3.141516; | |
| let str_pi: String = format!("{:.2}", pi); | |
| println!("num = {num}"); | |
| println!("pi = {pi}"); | |
| println!("approximate pi = {:.2}", pi); | |
| println!("approximate pi string = {str_pi}"); | |
| } | |
| //3. Conditionals as expressions | |
| fn conditional_expressions() { | |
| let body_temp: f32 = 101.43; | |
| let priority: u8; | |
| let (priority, fever_category) = if body_temp < 97.0 { | |
| (1, "Below normal") | |
| } else if body_temp <= 99.0 { | |
| (4, "Normal") | |
| } else if body_temp <= 100.4 { | |
| (3, "Low-grade fever") | |
| } else if body_temp <= 102.2 { | |
| (2, "Moderate fever") | |
| } else if body_temp <= 104.0 { | |
| (1, "High fever") | |
| } else { | |
| (1, "Very high fever") | |
| }; | |
| println!("{fever_category}"); | |
| let priority_str: &str = match priority { | |
| 1 => "Urgent & Important", | |
| 2 => "Not Urgent & Important", | |
| 3 => "Urgent & Not Important", | |
| 4 => "Not Urgent & Not Important", | |
| _ => "Unknown priority", | |
| }; | |
| println!("{priority_str}"); | |
| } | |
| //4. Conditionals as statements | |
| fn conditional_statements() { | |
| let body_temp: f32 = 101.43; | |
| let priority: u8; | |
| if body_temp < 97.0 { | |
| priority = 1; | |
| println!("Below normal"); | |
| } else if body_temp <= 99.0 { | |
| priority = 4; | |
| println!("Normal"); | |
| } else if body_temp <= 100.4 { | |
| priority = 3; | |
| println!("Low-grade fever"); | |
| } else if body_temp <= 102.2 { | |
| priority = 2; | |
| println!("Moderate fever"); | |
| } else if body_temp <= 104.0 { | |
| priority = 1; | |
| println!("High fever"); | |
| } else { | |
| priority = 1; | |
| println!("Very high fever"); | |
| } | |
| match priority { | |
| 1 => println!("Urgent & Important"), | |
| 2 => println!("Not Urgent & Important"), | |
| 3 => println!("Urgent & Not Important"), | |
| 4 => println!("Not Urgent & Not Important"), | |
| _ => println!("Unknown priority"), | |
| } | |
| } | |
| //5. while loop | |
| fn while_loop() { | |
| let mut i: u32 = 0; | |
| let mut sum: u32 = 0; | |
| while i <= 10 { | |
| sum += i; | |
| i += 1; | |
| } | |
| println!("{sum}"); | |
| } | |
| //6. functions: pass by value and return | |
| fn f(mut x: i32) -> i32 { | |
| x = 0; | |
| x | |
| } | |
| fn pass_by_value() { | |
| let mut x: i32 = 45; | |
| let y = f(x); | |
| println!("{x}, {y}"); | |
| } | |
| //7. functions: pass by reference | |
| fn g(x: &mut i32) { | |
| *x = 0; | |
| } | |
| fn pass_by_ref() { | |
| let mut x: i32 = 45; | |
| g(&mut x); | |
| println!("{x}"); | |
| } | |
| //8. structs, impl, traits | |
| use std::fmt; | |
| struct Point { | |
| x: f64, | |
| y: f64, | |
| } | |
| impl Point { | |
| fn distance(&self) -> f64 { | |
| (self.x.powi(2) + self.y.powi(2)).sqrt() | |
| } | |
| } | |
| impl fmt::Display for Point { | |
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| write!(f, "Point({}, {})", self.x, self.y) | |
| } | |
| } | |
| fn structs() { | |
| let p = Point { x: 3.0, y: 4.0 }; | |
| println!("{}", p); | |
| println!("Distance from origin: {}", p.distance()); | |
| } | |
| //9. traits | |
| trait Area { | |
| fn area(&self) -> f64; | |
| } | |
| struct Circle { | |
| radius: f64, | |
| } | |
| impl Area for Circle { | |
| fn area(&self) -> f64 { | |
| std::f64::consts::PI * self.radius * self.radius | |
| } | |
| } | |
| struct Rectangle { | |
| width: f64, | |
| height: f64, | |
| } | |
| impl Area for Rectangle { | |
| fn area(&self) -> f64 { | |
| self.width * self.height | |
| } | |
| } | |
| enum Shape { | |
| Circle(Circle), | |
| Rectangle(Rectangle), | |
| } | |
| impl Area for Shape { | |
| fn area(&self) -> f64 { | |
| match self { | |
| Shape::Circle(c) => c.area(), | |
| Shape::Rectangle(r) => r.area(), | |
| } | |
| } | |
| } | |
| fn traits() { | |
| let c = Shape::Circle(Circle { radius: 3.0 }); | |
| let r = Shape::Rectangle(Rectangle { | |
| width: 4.0, | |
| height: 5.0, | |
| }); | |
| println!("Circle area: {}", c.area()); | |
| println!("Rectangle area: {}", r.area()); | |
| } | |
| //10. ownership | |
| fn ownership_error() { | |
| //uncommenting this block will lead to compilation error | |
| /* | |
| let s1 = String::from("hello"); | |
| let s2 = s1; | |
| println!("{s1}, world!"); | |
| println!("{s2}, world!"); | |
| */ | |
| } | |
| fn borrow() { | |
| let s1 = String::from("hello"); | |
| let s2 = &s1; | |
| println!("{s1}, world!"); | |
| println!("{s2}, world!"); | |
| } | |
| fn deepcopy() { | |
| let s1 = String::from("hello"); | |
| let s2 = s1.clone(); | |
| println!("{s1}, world!"); | |
| println!("{s2}, world!"); | |
| } | |
| fn ownership() { | |
| ownership_error(); | |
| borrow(); | |
| deepcopy(); | |
| } | |
| //11. borrowing | |
| fn calculate_length(s: &String) -> usize { | |
| // s is an immutable reference; we can read but not modify | |
| s.len() | |
| } | |
| fn change(some_string: &mut String) { | |
| // some_string is a mutable reference; we are allowed to modify | |
| some_string.push_str(", world"); | |
| } | |
| fn no_dangle() -> String { | |
| let s = String::from("hello"); | |
| s // ownership is moved out; no dangling references | |
| } | |
| fn borrowing() { | |
| // Case 1: Immutable Borrowing (read-only) | |
| let s1 = String::from("hello"); | |
| let len = calculate_length(&s1); | |
| println!("The length of '{}' is {}", s1, len); | |
| // Case 2: Mutable Borrowing (allowing modification) | |
| let mut s2 = String::from("hello"); | |
| change(&mut s2); | |
| println!("After change: {}", s2); | |
| // Case 3: Conflicts between immutable and mutable borrows | |
| let mut s3 = String::from("hello"); | |
| // Two immutable references are okay: | |
| let r1 = &s3; | |
| let r2 = &s3; | |
| println!("Immutable borrows: {}, {}", r1, r2); | |
| // Now, mutable borrow after immutable borrows’ last use is okay: | |
| let r3 = &mut s3; | |
| r3.push_str(", world"); | |
| println!("Mutable after readonlys: {}", r3); | |
| // Case 4: Disallowed simultaneous mutable borrows | |
| /* | |
| let mut s4 = String::from("hello"); | |
| let m1 = &mut s4; | |
| let m2 = &mut s4; // ERROR: cannot borrow `s4` as mutable more than once at a time | |
| println!("{}, {}", m1, m2); | |
| */ | |
| // Case 5: Disallowed simultaneous mutable and immutable borrows | |
| /* | |
| let mut a = String::from("hello"); | |
| let ra1 = &a; | |
| let ra2 = &a; | |
| let ra3 = &mut a; | |
| ra3.push_str(", world"); | |
| println!("Unmodified a: {}, {}", ra1, ra2); | |
| println!("Modified a: {}", ra3); | |
| */ | |
| // Case 5: Avoiding dangling references | |
| let s5 = no_dangle(); | |
| println!("Got owned string: {}", s5); | |
| } | |
| //main | |
| fn main() { | |
| nod(); | |
| printing(); | |
| conditional_expressions(); | |
| conditional_statements(); | |
| while_loop(); | |
| pass_by_value(); | |
| pass_by_ref(); | |
| structs(); | |
| traits(); | |
| ownership(); | |
| borrowing(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment