examples/error-handling/Cargo.toml
[package]
name = "error-handling"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
examples/error-handling/src/main.rs
fn main() {
let args = std::env::args().collect::<Vec<String>>();
if args.len() != 2 {
eprintln!("Usage: {} FILENAME", args[0]);
std::process::exit(1);
}
let filename = &args[1];
println!("{}", divide(filename));
}
fn divide(filename: &str) -> Result<i32, std::io::Error> {
let content = std::fs::read_to_string(filename)?;
let content = content.strip_suffix('\n').unwrap();
let (dividend, divisor) = content.split_once(',').unwrap();
let dividend = dividend.parse::<i32>().unwrap();
let divisor = divisor.parse::<i32>().unwrap();
dividend / divisor
}
// fn divide(filename: &str) -> i32 {
// let content = std::fs::read_to_string(filename).unwrap();
// let content = content.strip_suffix('\n').unwrap();
// let (dividend, divisor) = content.split_once(',').unwrap();
// let dividend = dividend.parse::<i32>().unwrap();
// let divisor = divisor.parse::<i32>().unwrap();
// dividend / divisor
// }
examples/error-handling/one.txt
1
examples/error-handling/two.txt
30,2
examples/error-handling/zero.txt
30,0
examples/error-handling/forty_two.txt
forty,two