Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust_session/src/array.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[allow(dead_code)]
pub fn test_array() {
let arr: [u8; 3] = [10, 7, 10];
println!("array here: {:?}", arr);
Expand Down
36 changes: 30 additions & 6 deletions rust_session/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
mod sub;
mod sum;
mod ownership;
mod array;
mod mut_ex;
use crate::todo::Todo;

// mod sub;
// mod sum;
// mod ownership;
// mod array;
// mod mut_ex;
mod voters;
mod todo;



fn main() {
Expand All @@ -13,5 +18,24 @@ fn main() {
// array::test_array();
// ownership::test_move();
// ownership::call_greet();
mut_ex::test_mut();
// mut_ex::test_mut();
voters::check_voter_eligibility(20);

let todo1 = Todo::create_todo(
1,
"Buy milk".to_string(),
"Get 2 liters of whole milk".to_string(),
todo::Status::Pending,
);
println!("{:?}", todo1);

let todo2 = Todo::add_todo(
1, // current highest ID
"Walk the dog".to_string(),
"Around the block twice".to_string(),
todo::Status::Ongoing,
);
println!("{:?}", todo2);
}


6 changes: 5 additions & 1 deletion rust_session/src/ownership.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#[allow(dead_code)]
pub fn test_ownership() {
let name = String::from("Alice"); // `name` is the owner of this String
// name.push('r');
println!("name is: {}", name);
}

#[allow(dead_code)]
pub fn call_name() {
let name: &str = "Yusrah";
// name.

println!("name is: {}", name);
}


#[allow(dead_code)]
pub fn test_move() {
let a = String::from("Anagkazo");
println!("{}", a.clone()); // ✅ compiles: another address has been created in memory
Expand All @@ -21,10 +23,12 @@ pub fn test_move() {
println!("{}", b); // ✅ `b` is the owner now
}

#[allow(dead_code)]
pub fn greet(s: String) { // `s` takes ownership
println!("Hello, {}!", s);
} // `s` is dropped here

#[allow(dead_code)]
pub fn call_greet() {
let name = String::from("Bob");
greet(name); // ownership moves into `greet`
Expand Down
1 change: 1 addition & 0 deletions rust_session/src/sub.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[allow(dead_code)]
pub fn sub(x: u8, y: u8) -> u8 {
let result = x - y;
println!("result is: {result}");
Expand Down
1 change: 1 addition & 0 deletions rust_session/src/sum.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[allow(dead_code)]
fn sum(x: u8, y: u8) -> u8 {
x + y
// return x + y;
Expand Down
42 changes: 42 additions & 0 deletions rust_session/src/todo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#[derive(Debug, PartialEq)]
#[allow(dead_code)]
pub enum Status {
Pending,
Ongoing,
Completed,
Cancel,
}

#[derive(Debug)]
#[allow(dead_code)]
pub struct Todo {
id: u8,
title: String,
description: String,
status: Status,
}

impl Todo {
pub fn create_todo(id: u8, title: String, description: String, status: Status) -> Todo {
let todo: Todo = Todo {
id,
title,
description,
status
};
println!("You have created a new todo");
return todo;
}

pub fn add_todo(current_id: u8, title: String, description: String, status: Status) -> Todo {
let new_id: u8 = current_id + 1;
let todo: Todo = Todo {
id: new_id,
title,
description,
status
};
println!("You have added a new todo with ID: {}", new_id);
return todo;
}
}
65 changes: 65 additions & 0 deletions rust_session/src/voters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#[derive(Debug)] // It can be called a macro, attribute or annotation
pub enum AgeGroup {
Juvenile,
Silver,
Golden,
Platinum,
Centurion,
}

impl AgeGroup {
pub fn from_age(age: u32) -> AgeGroup {
match age {
0..=17 => {
println!("Go back home!");
AgeGroup::Juvenile
}
18..=25 => {
println!("You're above the age of 18");
AgeGroup::Silver
}
26..=50 => {
println!("You're above the age of 25.");
AgeGroup::Golden
}
51..=75 => {
println!("You're above the age of 50.");
AgeGroup::Platinum
}
_ => {
println!("You're above the age of 75 now.");
AgeGroup::Centurion
}

}
}
}


#[derive(Debug)]
pub enum VoterEligibility {
Eligible,
NotEligible,
}

#[allow(dead_code)]
impl VoterEligibility {
pub fn is_eligible(group: &AgeGroup) -> VoterEligibility {
match group {
AgeGroup::Juvenile => VoterEligibility::NotEligible,
_ => VoterEligibility::Eligible,
}
}

pub fn description(&self) -> &str {
match self {
VoterEligibility::Eligible => "This voter is eligible.",
VoterEligibility::NotEligible => "You're not eligible to VOTE!",
}
}
}

pub fn check_voter_eligibility(age: u32) {
let age_group = AgeGroup::from_age(age);
println!("You're in the age group of {:?}", age_group);
}
Loading