first commit
This commit is contained in:
commit
a66604eb6c
52 changed files with 790 additions and 0 deletions
7
options-results/Cargo.lock
generated
Normal file
7
options-results/Cargo.lock
generated
Normal 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"
|
||||
6
options-results/Cargo.toml
Normal file
6
options-results/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "options-results"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
40
options-results/src/main.rs
Normal file
40
options-results/src/main.rs
Normal 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)
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue