r4_grrs

Crates.ior4_grrs
lib.rsr4_grrs
version0.1.1
sourcesrc
created_at2023-06-14 10:44:06.096966
updated_at2023-06-14 10:49:05.612828
descriptionA tool to search files
homepagehttps://github.com/chengr4/my-first-cli-tool
repositoryhttps://github.com/chengr4/my-first-cli-tool
max_upload_size
id890004
size23,645
Eddie Cheng R4 (chengr4)

documentation

README

my-first-cli-tool

Have Learned

  • BufReader is a better choise than read_to_string
  • Instead of the panic!, we can also easily write return
let result = std::fs::read_to_string("test.txt");
let content = match result {
    Ok(content) => { content },
    Err(error) => { return Err(error.into()); }
};
  • unwrap() is a shortcut for the match with panic!
  • Box<dyn std::error::Error>: It’s a Box that can contain any type that implements the standard Error trait. All errors can be put into this box
fn main() -> Result<(), Box<dyn std::error::Error>> {
    // `?` expands to code that converts error types and returns error
    let content = std::fs::read_to_string("test.txt")?;
    println!("file content: {}", content);
    Ok(())
}

  • println! tells the system to flush to the terminal every time (from buffer to terminal)
  • By convention, cargo will look for integration tests in the tests/ directory.

References

Commit count: 11

cargo fmt