Propagando Erros com ?
O operador try ? é usado para retornar erros ao chamador da função. Se ocorrer um erro, este é retornado imediatamente ao chamador como retorno da função.
match some_expression {
Ok(value) => value,
Err(err) => return Err(err),
}
O código acima pode ser simplificado para:
some_expression?
Podemos usar isso para simplificar nosso código de tratamento de erros:
use std::{fs, io}; use std::io::Read; fn read_username(path: &str) -> Result<String, io::Error> { let username_file_result = fs::File::open(path); let mut username_file = match username_file_result { Ok(file) => file, Err(err) => return Err(err), }; let mut username = String::new(); match username_file.read_to_string(&mut username) { Ok(_) => Ok(username), Err(err) => Err(err), } } fn main() { //fs::write("config.dat", "alice").unwrap(); let username = read_username("config.dat"); println!("nome_usuario ou erro: {username:?}"); }
Pontos chave:
- A variável
nome_usuariopode serOk(string)ouErr(error). - Use a chamada
fs::writepara testar os diferentes cenários: nenhum arquivo, arquivo vazio e arquivo com nome de usuário. - The return type of the function has to be compatible with the nested functions it calls. For instance, a function returning a
Result<T, Err>can only apply the?operator on a function returning aResult<AnyT, Err>. It cannot apply the?operator on a function returning anOption<AnyT>orResult<T, OtherErr>unlessOtherErrimplementsFrom<Err>. Reciprocally, a function returning anOption<T>can only apply the?operator on a function returning anOption<AnyT>.- You can convert incompatible types into one another with the different
OptionandResultmethods such asOption::ok_or,Result::ok,Result::err.
- You can convert incompatible types into one another with the different