2 releases
0.1.1 | Jun 14, 2023 |
---|---|
0.1.0 | Jun 14, 2023 |
#81 in #demo
8KB
my-first-cli-tool
- Learn from https://github.com/rust-cli/book
Have Learned
BufReader
is a better choise thanread_to_string
- Instead of the
panic!
, we can also easily writereturn
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 withpanic!
Box<dyn std::error::Error>
: It’s a Box that can contain any type that implements the standardError
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
Dependencies
~1.2–1.8MB
~34K SLoC