将错误消息返回到需要'Box '

问题描述 投票:0回答:1

我对Rust还是陌生的,正在尝试传播要在调用函数中处理的错误。从官方的锈书中,我读到结果'Box '用来表示捕获任何类型的错误,但是我读得不够多,无法理解它的实际工作方式。

我有一个称为:的功能

fn foo() -> Result<String, Box<dyn Error>> {
    Command::new("an_executable")
        .args(&["-p", path])
        .output()?;
    if condition {
        return Err("Error...");
    }
    // Do stuff, return String 
}

所以有人可以解释如果该返回类型满足条件,我应该如何返回错误。我必须更改返回类型还是只返回其他内容。在这种情况下,RUST标准是什么?

当前的编译错误是Err(“ Error ...”)与返回类型不匹配

error-handling rust
1个回答
0
投票

让我们从最小限度地复制您的问题开始:

use std::error::Error;

fn foo() -> Result<String, Box<dyn Error>> {
    Err("Error...")
}

返回的错误是:

error[E0308]: mismatched types
 --> src/lib.rs:4:9
  |
4 |     Err("Error...")
  |         ^^^^^^^^^^ expected struct `std::boxed::Box`, found reference
  |
  = note: expected type `std::boxed::Box<dyn std::error::Error>`
             found type `&'static str`

是说函数签名希望您返回包含ErrBox,但实际上您返回了&str。因此,我们将字符串装箱:

use std::error::Error;

fn foo() -> Result<String, Box<dyn Error>> {
    Err(Box::new("Error..."))
}

现在出现新错误:

error[E0277]: the trait bound `&str: std::error::Error` is not satisfied
 --> src/lib.rs:4:9
  |
4 |     Err(Box::new("Error..."))
  |         ^^^^^^^^^^^^^^^^^^^^ the trait `std::error::Error` is not implemented for `&str`
  |
  = note: required for the cast to the object type `dyn std::error::Error`

同样,问题是您返回的类型与函数签名不符-期望该框包含实现Error特性的内容,但是如果您look at the docs,您会注意到[ C0]不是实现它的类型之一。您需要将字符串包装成确实实现&str的类型:

Error

此代码编译!

EDIT:我刚刚发现自己有一种更简单的方法可以执行此操作-use std::error::Error; use std::fmt; #[derive(Debug)] struct StrError<'a>(&'a str); // Error doesn't require you to implement any methods, but // your type must also implement Debug and Display. impl<'a> Error for StrError<'a> {} impl<'a> fmt::Display for StrError<'a>{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // Delegate to the Display impl for `&str`: self.0.fmt(f) } } fn foo() -> Result<String, Box<dyn Error>> { Err(Box::new(StrError("Error..."))) } From<&str>实现,因此,如果调用该实现,则可以避免上述样板。不过,相同的事情或多或少正在发生,我希望更详细的解释会有所帮助!。

Box<dyn Error>
© www.soinside.com 2019 - 2024. All rights reserved.