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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ lib/
# Rust build artifacts
/target
**/target
**/Cargo.lock

# Personal notes
rust_session/student_registry_v2/notes/

# Hardhat / Foundry build artifacts
artifacts/
Expand Down
7 changes: 0 additions & 7 deletions rust_session/Cargo.lock

This file was deleted.

31 changes: 31 additions & 0 deletions rust_session/src/grade.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[derive(Debug)]
pub enum Grade {
First,
Second,
Third
}

impl Grade {
pub fn get_grade_points(&self) -> &str {
match self {
Grade::First => "Cohort 1",
Grade::Second => "Cohort 2",
Grade::Third => "Cohort 3",
}
}
}

#[derive(Debug)]
pub enum Sex {
Male,
Female
}

impl Sex {
pub fn get_sex(&self) {
match self {
Sex::Male => println!("Male"),
Sex::Female => println!("Female"),
}
}
}
35 changes: 28 additions & 7 deletions rust_session/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
mod sub;
mod sum;
mod ownership;
mod array;
mod mut_ex;

// mod sub;
// mod sum;
// mod ownership;
// mod array;
// mod mut_ex;
// mod grade;
// mod voters;
// mod student_struct;

fn main() {
// use sub::sub;
// use crate::voters::{Age, EligibleVoters};
// sub(10, 5);
// ownership::test_ownership();
// ownership::call_name();
// array::test_array();
// ownership::test_move();
// ownership::call_greet();
mut_ex::test_mut();
// mut_ex::test_mut();
// grade::Grade::First.get_grade_points();
// grade::Grade::Second.get_grade_points();
// grade::Grade::Third.get_grade_points();
// grade::Sex::Male.get_sex();
// grade::Sex::Female.get_sex();
// voters::Age::SilverJubilee.get_age();
// voters::Age::GoldenJubilee.get_age();
// voters::Age::PlatinumJubilee.get_age();
// voters::Age::Centenarian.get_age();
// voters::EligibleVoters::Juvenile.get_eligible_voters();
// voters::EligibleVoters::Teenager.get_eligible_voters();
// voters::EligibleVoters::Adult.get_eligible_voters();

// let age = Age::SilverJubilee;
// let eligible_voters = EligibleVoters::Teenager;
// println!("Age: {}", age.get_age());
// println!("Eligible Voters: {}", eligible_voters.get_eligible_voters());

}
37 changes: 37 additions & 0 deletions rust_session/src/student_struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#[derive(Debug)]
pub enum Status{
Pending,
Completed,
InProgress,
Cancelled,
}

impl Status {
pub fn get_status(&self) -> &str {
match self {
Status::Pending => "Pending",
Status::Completed => "Completed",
Status::InProgress => "InProgress",
Status::Cancelled => "Cancelled",
}
}
}


pub struct Todo {
pub id: u8,
pub title: String,
pub description: String,
pub status: Status,
}

impl Todo {
pub fn new(id: u8, title: String, description: String, status: Status) -> Todo {
Todo {
id,
title,
description,
status
}
}
}
35 changes: 35 additions & 0 deletions rust_session/src/voters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#[derive(Debug)]
pub enum Age {
SilverJubilee,
GoldenJubilee,
PlatinumJubilee,
Centenarian
}

impl Age {
pub fn get_age(&self) -> &str {
match self {
Age::SilverJubilee => "25 years",
Age::GoldenJubilee => "50 years",
Age::PlatinumJubilee => "75 years",
Age::Centenarian => "100 years",
}
}
}

#[derive(Debug)]
pub enum EligibleVoters {
Juvenile,
Teenager,
Adult
}

impl EligibleVoters {
pub fn get_eligible_voters(&self) -> &str {
match self {
EligibleVoters::Juvenile => "17 years and below",
EligibleVoters::Teenager => "18 years and above",
EligibleVoters::Adult => "35 years and above",
}
}
}
7 changes: 0 additions & 7 deletions rust_session/student_registry/Cargo.lock

This file was deleted.

59 changes: 37 additions & 22 deletions rust_session/student_registry/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
mod grade;
mod registry;
mod student_struct;
mod utils;
// mod utils;

use grade::{Grade, Sex};
use registry::Registry;
use student_struct::Student;
// use student_struct::Student;

fn main() {
// let g = Grade::Second;
Expand All @@ -15,29 +15,44 @@ fn main() {
// let ss = Student::new();
// println!("stund")

// let mut reg = Registry::new();
let mut reg = Registry::new();

reg.add("Victor", 20, Sex::Male, Grade::First, 78.5,);
reg.add("Kosi", 22, Sex::Female, Grade::Second, 64.0);
reg.add("Yusrah", 21, Sex::Female, Grade::First, 91.0);

// reg.add("Victor", 20, Grade::First, 78.5);
// reg.add("Kosi", 22, Grade::Second, 64.0);
// reg.add("Yusrah", 21, Grade::First, 91.0);
reg.list_all();

// reg.list_all();]

let sex = Sex::Male;
println!("sex: {:?}", sex.to_str());
let second_id = reg.students[1].id.clone();

// let s: Student = Student::new(1, String::from("Testimony"), 16, Sex::Female, Grade::Third, 40.5);
let s: Student = Student::new(
1,
"Testimony".to_string(),
16,
Sex::Female,
Grade::Third,
40.5,
);
println!("student here: {:#?}", s);
match reg.find_by_id(second_id) {
Some(student) => println!("Found student: {} with ID: {}", student.name, student.id),
None => println!("Student not found"),
}

println!("student id: {}", s.id);
println!("student name: {}", s.name);
println!("student age: {}", s.age);
reg.update(second_id, "joy", 25, 50.0);
println!();
reg.list_all();




// let sex = Sex::Male;
// println!("sex: {:?}", sex.to_str());

// // let s: Student = Student::new(1, String::from("Testimony"), 16, Sex::Female, Grade::Third, 40.5);
// let s: Student = Student::new(
// 1,
// "Testimony".to_string(),
// 16,
// Sex::Female,
// Grade::Third,
// 40.5,
// );
// println!("student here: {:#?}", s);

// println!("student id: {}", s.id);
// println!("student name: {}", s.name);
// println!("student age: {}", s.age);
}
31 changes: 31 additions & 0 deletions rust_session/student_registry/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ pub struct Registry {
}

impl Registry {
pub fn new() -> Registry {
Registry {
students: Vec::new(),
next_id: 0,
}
}

pub fn add(&mut self, name: &str, age: u8, sex: Sex, grade: Grade, score: f32) {
let id = self.next_id;
let student = Student::new(id, name.to_string(), age, sex, grade, score);
Expand Down Expand Up @@ -36,4 +43,28 @@ impl Registry {
);
}
}

pub fn find_by_id(&self, id: u32) -> Option<&Student> {
self.students.iter().find(|s| s.id == id)
}
// - .iter() loops through the vec without taking ownership
// - .find() returns the first match wrapped in Option<&Student>
// - Option means it can be Some(student) if found, or None if not — Rust forces you to handle both cases, no null panics

pub fn delete(&mut self, id: u32) {
self.students.retain(|s| s.id != id);
}
// - .retain() keeps only elements where the condition is true
// - So s.id != id means "keep everyone whose id is NOT the one we want to remove"
// - It mutates the vec in place — no need to find an index

pub fn update(&mut self, id: u32, name: &str, age: u8, score: f32) {
if let Some(student) = self.students.iter_mut().find(|s| s.id == id) {
student.name = name.to_string();
student.age = age;
student.score = score;
}
}
// - .iter_mut() gives mutable references so you can modify fields in place
// - if let Some(student) is Rust's clean way to say "if found, do this, otherwise skip"
}
7 changes: 7 additions & 0 deletions rust_session/student_registry_v2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "student_registry_v2"
version = "0.1.0"
edition = "2021"

[dependencies]
uuid = { version = "1", features = ["v4"] }
31 changes: 31 additions & 0 deletions rust_session/student_registry_v2/src/grade.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[derive(Debug, PartialEq)]
pub enum Grade {
First,
Second,
Third,
}

impl Grade {
pub fn as_str(&self) -> &str {
match self {
Grade::First => "Cohort 1",
Grade::Second => "Cohort 2",
Grade::Third => "Cohort 3",
}
}
}

#[derive(Debug)]
pub enum Sex {
Male,
Female,
}

impl Sex {
pub fn to_str(&self) -> &str {
match self {
Sex::Male => "male",
Sex::Female => "female",
}
}
}
49 changes: 49 additions & 0 deletions rust_session/student_registry_v2/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
mod grade;
mod registry;
mod student_struct;

use grade::{Grade, Sex};
use registry::Registry;

fn section(title: &str) {
let width = 60;
println!("\n{}", "=".repeat(width));
println!("{:^width$}", title, width = width);
println!("{}", "=".repeat(width));
}

fn main() {
let mut reg = Registry::new();

section("REGISTERING STUDENTS");
reg.add("Victor", 20, Sex::Male, Grade::First, 78.5);
reg.add("Kosi", 22, Sex::Female, Grade::Second, 64.0);
reg.add("Yusrah", 21, Sex::Female, Grade::First, 91.0);

section("ALL STUDENTS");
reg.list_all();

let first_id = reg.students[0].id.clone();

// section("FIND BY ID");
// match reg.find_by_id(&first_id) {
// Some(s) => {
// println!(" Name : {}", s.name);
// println!(" ID : {}", s.id);
// println!(" Age : {}", s.age);
// println!(" Sex : {}", s.sex.to_str());
// println!(" Grade : {}", s.grade.as_str());
// println!(" Score : {:.1}", s.score);
// }
// None => println!(" Not found"),
// }

// section("UPDATE STUDENT");
// reg.update(&first_id, "Victor Updated", 21, 85.0);
// println!();
// reg.list_all();

section("DELETE STUDENT");
reg.delete(&first_id);
reg.list_all();
}
Loading
Loading