![]() ![]() expect() also desugars to a panic!() but prints out an error message too. (Recall that if we omit the semicolon a function returns the value of its last line.) use std::io::Write // needed for write!()įn hello(filename: &std::path::Path) -> Result Aha! unwrap() desugars to a panic!(), something that (as the title of this section implies) you should not do. The Result is returned to main() where, good or bad, we will have to deal with it. The write!() macro returns a Result that is Ok(()) if the writing was succesful and Err( std::io::Error) if there was a problem. The real action happens at line 5 where we call the write!() macro to actually write "Hello world!" to the file. We define a function hello() that performs the actual logic of creating and writing to hello.txt and is called by main(). This simple example introduces the question mark operator (formerly the try!() macro) and demonstrates correct, idiomatic error propagation in Rust.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |