简体   繁体   English

结果的 Rust 简写<type, Box<dyn std::error::Error> &gt;

[英]Rust shorthand for Result<type, Box<dyn std::error::Error>>

When doing error catching I usually make a function return a result.在进行错误捕获时,我通常使函数返回结果。 But I feel like writing Result<type, Box<...>> everytime is really verbose, is there some built-in shorthand for this?但是我觉得每次都写 Result<type, Box<...>> 真的很冗长,是否有一些内置的速记?

fn something() -> Result<(), Box<dyn std::error::Error>> {
  Ok(())
}

You can just define a type alias with generic arguments.您可以使用泛型参数定义类型别名。 Many crates do like this:许多板条箱是这样的:

type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

fn something() -> Result<()> {
  Ok(())
}

The anyhow crate, written by the author of serde, is designed around an ergonomic alternative to Box<dyn std::error::Error> called anyhow::Error .anyhow板条箱,通过SERDE的作者写的,是围绕到符合人体工程学的替代Box<dyn std::error::Error>称为anyhow::Error It defines anyhow::Result<T> as alias for Result<T, anyhow::Error> :它定义了anyhow::Result<T>作为Result<T, anyhow::Error>别名:

fn something() -> anyhow::Result<()> {
    Ok(())
}

The downside is that it's an external crate, although a very popular and well-tested one.缺点是它是一个外部板条箱,尽管它非常受欢迎且经过充分测试。

The upside is that you get good ergonomics, additional features (such as context() and with_context() on Result ), as well as non-trivial optimizations - anyhow::Error is a narrow pointer rather than a wide one, so your Result s are smaller and more efficient compared to Box<dyn Error> .好处是您可以获得良好的人体工程学、附加功能(例如Result上的context()with_context() ),以及非平凡的优化 - anyhow::Error是一个窄指针而不是一个宽指针,因此您的ResultBox<dyn Error>相比,s 更小更高效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 返回结果&lt;(), 框<dyn Error> &gt; 生锈 - Returning Result<(), Box<dyn Error>> in rust 如何泛化 Rust 结果的错误<T, E>导致<T, Box<dyn std::error::Error> &gt;? - How do I generify the error of a Rust Result<T, E> to Result<T, Box<dyn std::error::Error>>? Rust 错误:在编译时无法知道类型为 `(dyn std::error::Error + &#39;static)` 的值的大小 - Rust Error: The size for values of type `(dyn std::error::Error + 'static)` cannot be known at compilation time Rust:如何定义一个可以返回任一结果的闭包<T, MyErrorType>或结果<T, Box<dyn Error> &gt; - Rust: How to define a closure which can return either Result<T, MyErrorType> or Result<T, Box<dyn Error>> 为什么特征类型为`Box<dyn error> ` 错误“未实现大小”但 `async fn() -&gt; Result&lt;(), Box<dyn error> &gt;` 有效吗?</dyn></dyn> - Why does a trait type `Box<dyn Error>` error with "Sized is not implemented" but `async fn() -> Result<(), Box<dyn Error>>` works? 如何测试 Box 中错误的类型<dyn Error> ? - How to test the type of an error in Box<dyn Error>? 如何手动返回 Result&lt;(), Box<dyn Error> &gt;? - How to manually return a Result<(), Box<dyn Error>>? 标准是否定义了 Result 的别名<T, dyn std::error::Error> ? - Does the standard define an alias to Result<T, dyn std::error::Error>? `dyn std::error::Error == dyn std::error::Error 没有实现 - no implementation for `dyn std::error::Error == dyn std::error::Error` 为什么没有盒子<dyn std::error::error>捕获 std::string::FromUtf8Error</dyn> - Why doesn't Box<dyn std::error::Error> capture std::string::FromUtf8Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM