first commit

This commit is contained in:
Ashborn 2025-12-16 22:10:06 +05:45
commit a66604eb6c
52 changed files with 790 additions and 0 deletions

7
options-results/Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "options-results"
version = "0.1.0"

View file

@ -0,0 +1,6 @@
[package]
name = "options-results"
version = "0.1.0"
edition = "2024"
[dependencies]

View file

@ -0,0 +1,40 @@
fn main() {
//Option and Result are enums used to handle absence of a value and error handling,
//respectively, withoug using null or exceptions
match get_username(1) {
Some(name)=>println!("{}", name),
None => println!("user not found")
}
match safe_divide(23, 2){
Ok(result)=>println!("{}", result),
Err(e)=> println!("{}", e)
}
match safe_divide(23, 0){
Ok(result)=>println!("{}", result),
Err(e)=> println!("{}", e)
}
}
fn get_username(id:u32)->Option<&'static str>{
if id ==1{
Some("Safal")
}else{
None
}
}
fn safe_divide(a:i32, b:i32)->Result<i32, String>{
if b == 0 {
Err("Can't divide by zero".to_string())
}else {
Ok(a/b)
}
}