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
erro-handling/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 = "erro-handling"
version = "0.1.0"

6
erro-handling/Cargo.toml Normal file
View file

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

3
erro-handling/README.md Normal file
View file

@ -0,0 +1,3 @@
# for quick prototyping:
- let file = File::open("hello.txt").unwrap(); //panics
- let file = File::open("hello.txt").expect("Failed to open") // custom error

27
erro-handling/src/main.rs Normal file
View file

@ -0,0 +1,27 @@
use std::{fs::File, io::{self, Read}};
//Error handling in Rust is a critical Aspect of writing reliable and robust programs. Rust
//encuorages handling errors explicity and provides two main types
// - panic! macro -> for unrecovable errors
// - Result<T,E> enum -> for recovable errors
fn main() {
//always use panic when something goes wrong that the program cannot continue
// let num = vec![1,2,3];
// println!("{}", num[99]); //panics cus the index is out of bound!
//Result type is used to handle operations that can succeed or fail like file I/O
match read_file("hello.txt"){
Ok(contents)=> println!("{}", contents),
Err(e)=> eprint!("Erro readihg file: {}",e)
}
}
fn read_file(filename: &str)-> Result<String, io::Error>{
let mut file = File::open(filename)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}