尝试运算符

Runtime errors like connection-refused or file-not-found are handled with the Result type, but matching this type on every call can be cumbersome. The try-operator ? is used to return errors to the caller. It lets you turn the common

  1. match some_expression {
  2. Ok(value) => value,
  3. Err(err) => return Err(err),
  4. }

转换成更简单的命令

  1. some_expression?

We can use this to simplify our error handling code:

  1. use std::io::Read;
  2. use std::{fs, io};
  3. fn read_username(path: &str) -> Result<String, io::Error> {
  4. let username_file_result = fs::File::open(path);
  5. let mut username_file = match username_file_result {
  6. Ok(file) => file,
  7. Err(err) => return Err(err),
  8. };
  9. let mut username = String::new();
  10. match username_file.read_to_string(&mut username) {
  11. Ok(_) => Ok(username),
  12. Err(err) => Err(err),
  13. }
  14. }
  15. fn main() {
  16. //fs::write("config.dat", "alice").unwrap();
  17. let username = read_username("config.dat");
  18. println!("username or error: {username:?}");
  19. }

This slide should take about 5 minutes.

简化 read_username 函数以使用 ?

关键点:

  • username 变量可以是 Ok(string)Err(error)
  • 可以使用 fs::write 调用来测试不同的场景:没有文件、空文件、包含用户名的文件。
  • Note that main can return a Result<(), E> as long as it implements std::process::Termination. In practice, this means that E implements Debug. The executable will print the Err variant and return a nonzero exit status on error.