Skip to content

Commit

Permalink
Update 2024-01-28-lesson3.md
Browse files Browse the repository at this point in the history
  • Loading branch information
raikrahul committed Jan 25, 2025
1 parent 0224c26 commit ace1592
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions _posts/2024-01-28-lesson3.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,77 @@ fn main() {
};

println!("User {} has email {}", user1.username, user1.email);
}
impl User {
// Associated function (constructor)
fn new(email: String, username: String) -> User {
User {
email,
username,
active: true,
sign_in_count: 0,
}
}

// Method (takes &self)
fn deactivate(&mut self) {
self.active = false;
}
}

fn main() {
let mut user2 = User::new(
String::from("admin@rust.org"),
String::from("admin")
);

user2.deactivate();
println!("Account active: {}", user2.active);
}
enum WebEvent {
PageLoad,
PageUnload,
KeyPress(char),
Paste(String),
Click { x: i64, y: i64 },
}

fn inspect(event: WebEvent) {
match event {
WebEvent::PageLoad => println!("page loaded"),
WebEvent::PageUnload => println!("page unloaded"),
WebEvent::KeyPress(c) => println!("pressed '{}'", c),
WebEvent::Paste(s) => println!("pasted \"{}\"", s),
WebEvent::Click { x, y } => {
println!("clicked at x={}, y={}", x, y);
}
}
}

fn main() {
let click = WebEvent::Click { x: 20, y: 80 };
inspect(click);
}
enum Shape {
Circle(f64),
Rectangle(f64, f64),
Triangle(f64, f64, f64),
}

struct Geometry {
shape: Shape,
color: String,
}

impl Geometry {
fn area(&self) -> f64 {
match self.shape {
Shape::Circle(radius) => 3.14159 * radius * radius,
Shape::Rectangle(w, h) => w * h,
Shape::Triangle(a, b, c) => {
let s = (a + b + c) / 2.0;
(s * (s - a) * (s - b) * (s - c)).sqrt()
}
}
}
}

0 comments on commit ace1592

Please sign in to comment.