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 ler_usuario(caminho: &str) -> Result<String, io::Error> { let resultado_arq_usuario = fs::File::open(caminho); let mut arquivo_usuario = match resultado_arq_usuario { Ok(arquivo) => arquivo, Err(err) => return Err(err), }; let mut nome_usuario = String::new(); match arquivo_usuario.read_to_string(&mut nome_usuario) { Ok(_) => Ok(nome_usuario), Err(err) => Err(err), } } fn main() { //fs::write("config.dat", "alice").unwrap(); let nome_usuario = ler_usuario("config.dat"); println!("nome_usuario ou erro: {nome_usuario:?}"); }
Pontos chave:
- A variável
nome_usuario
pode serOk(string)
ouErr(error)
. - Use a chamada
fs::write
para 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>
unlessOtherErr
implementsFrom<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
Option
andResult
methods such asOption::ok_or
,Result::ok
,Result::err
.
- You can convert incompatible types into one another with the different