rust_programming_book 0.1.1

Programming works from THE RUST PROGRAMMING LANGUAGE
Documentation
use core::panic;
use std::fs::{self, File};
use std::io::ErrorKind;

#[allow(dead_code)]
fn unrecoverable() {
    panic!("Crashed!");
}
#[allow(dead_code, unused_variables)]
fn recoverable() {
    // handling recoverable errors with closure.
    let file = File::open("hello.txt").unwrap_or_else(|error| {
        if error.kind() == ErrorKind::NotFound {
            File::create("hello.txt").unwrap_or_else(|error| {
                panic!("Error in creating file:{:?}", error);
            })
        } else {
            panic!("Problem in opening file:{:?}", error);
        }
    });
}

#[allow(dead_code)]
fn mark_operator() -> Result<String, std::io::Error> {
    fs::read_to_string("hello.txt")

    // let mut f = File::open("hello.txt").expect("error while opening file");
    // or  let mut f = File::open("hello.txt")?;
    // let mut s = String::new();
    // f.read_to_string(&mut s).expect("error while reading file");
    // Ok(s)
}

#[allow(dead_code)]
pub fn error_handling() {
    // handling unrecoverable errors using panic
    // unrecoverable();
    // recoverable();

    println!("{:?}", mark_operator());
}