问题标签 [rust-result]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
4 回答
26480 浏览

rust - 当 Iterator::map 返回 Result::Err 时,如何停止迭代并返回错误?

我有一个返回 a 的函数Result

然后另一个像这样使用它:

如何处理任何map迭代中的失败情况?

我知道我可以使用flat_map,在这种情况下,错误结果将被忽略

Result的迭代器有 0 或 1 项,具体取决于成功状态,flat_map如果为 0,则将其过滤掉。

但是,我不想忽略错误,而是想让整个代码块停止并返回一个新错误(基于地图中出现的错误,或者只是转发现有错误)。

我如何在 Rust 中最好地处理这个问题?

0 投票
4 回答
27211 浏览

rust - 使用结果迭代器最惯用的方式是什么?

我有这样的代码:

在语义方面,我想在第一个错误之后停止处理。

上面的代码可以,但是感觉挺麻烦的。有没有更好的办法?我已经查看了类似的文档filter_if_ok,但我没有找到任何东西。

我知道collect::<Result<Vec<_>, _>>,而且效果很好。我特别想消除以下样板:

  • 在过滤器的关闭中,我必须使用matchon thing_result。我觉得这应该只是一个单行,例如.filter_if_ok(|thing| check(a))
  • 每次我使用map时,我都必须包含一个额外的语句let a = try!(thing_result);以处理Err. 同样,我觉得这可以抽象为.map_if_ok(|thing| ...).

我可以使用另一种方法来获得这种简洁程度,还是我只需要坚持下去?

0 投票
2 回答
14619 浏览

error-handling - What is the idiomatic way to return an error from a function with no result if successful?

In Rust, I believe the idiomatic way to deal with recoverable errors is to use Result. For example this function clearly is idiomatic:

Of course, there are also functions that have a single, obvious, failure state, and therefore use the Option type instead. An idiomatic example would be this:

This all is straightforwardly addressed in the documentation. However, I'm confused about the case where a function can fail, but has no meaningful value when successful. Compare the following two functions:

I'm just not sure which one of these is more idiomatic, or is used more often in real world Rust code. My go-to resource for questions like this is the Rust book, but I don't think this is addressed in its "Error Handling" section. I haven't had much luck with any other Rust documentation either.

Of course this seems pretty subjective, but I'm looking for authoritative sources that either state which form is idiomatic, or on why one form is superior (or inferior) to the other. (I'm also curious how the convention compares to other languages that heavily utilize "errors as values", like Go and Haskell.)

0 投票
1 回答
783 浏览

error-handling - 用 Result 包装非错误函数的惯用 Rust 方法是什么?

我有一个将 a 解析str为数字并返回它或错误的函数

SaleError是我的自定义错误结构。

解析后,我想在结果值中做一些其他检查,例如,检查数字是否为正数is_sign_positive(),如果不是,我想发出一个Err(SaleError).

由于is_sign_positive()只返回 a bool,我所做的是创建这个函数:

现在我可以这样使用它:

这很好用,但请注意,我想要一个特定的SaleError实例value1and value2,所以我使用了这个or()函数。

现在,由于我总是想要一个特定的错误而不是is_positive返回给我的错误,是否可以包装is_sign_positive()以便我可以在不需要创建函数的情况下使用它is_positive

像这样的东西:

0 投票
2 回答
1943 浏览

rust - 是否可以转换选项> 到一个结果, E> 不使用匹配?

我的第一个想法是mapOption但我不能try!从封闭内部使用。该match声明看起来没有必要,但我不知道如何简化它。

0 投票
2 回答
2252 浏览

rust - 在没有 map_err 的情况下将 and_then 与不同的 Result 错误类型一起使用

我有一些函数在失败时会返回不同的错误类型。

首先,我有一个构建器,其中包含此方法:

所以它会返回一个BuildError失败。

现在,我有另一个函数会返回另一个错误:

这个想法是我使用构建器来创建一个ServiceStatus对象并使用它来创建一个带有create_xml函数的 XML 字符串。

为此,我有以下代码:

现在,我认为我可以做得更好 usingand_then而不是 using ? 操作员:

该解决方案也有效,但现在我需要显式调用map_err以从 aBuilderError转换XmlErrorWebserviceError...

所以,我的问题是,我能做得更好吗?我认为这样的解决方案将是理想的:

0 投票
2 回答
7083 浏览

rust - Rust 从 fn 返回结果错误:类型不匹配

我希望这个函数返回一个错误结果:

错误信息

我很困惑如何在使用预期的结构时打印出错误消息。

0 投票
3 回答
4444 浏览

rust - 使用返回 Result 的闭包折叠

我正在使用 regex crate 用这个 regex 查找一些文本:

我想找到所有可能的捕获并遍历它们:

每个捕获的元素由 2 个值组成:一个操作和一个数字。例如,输出可能是:

我想迭代它,解析数字并应用操作。

我试过了:

哪里apply_decoding_step是:

但我得到了这个错误:

我认为这是因为我试图将 a 折叠Result成 a i32,但由于我需要解析第二个捕获值并且还需要otherwise在我的情况下,我match该如何解决这个问题?

0 投票
1 回答
179 浏览

generics - Extending all Iterators> to transform type

I'm trying to implement an extension trait for all Iterator<Item = Result<Type, E>> where E is generic, to generate another Iterator over Result<OtherType, E>, where the errors from the original are forwarded.

The problem is, that the transformation Type -> OtherType may fail (the function is f(t: Type) -> Result<OtherType, ConcreteError>.

Therefor, the iteration might return E (generic) from the underlying iterator or a concrete error type, which is of course not possible.

How to implement this?

A minimal example:

playground

Errors:

0 投票
1 回答
625 浏览

generics - 确定类型时 match 和 unwrap_or 之间的区别

以下是我可以使用 Rust 1.23.0 编译的有效文件:

虽然下面的代码

失败并出现错误:

据我所知,编译器在这里确定类型时的推理如下:

  1. match它确定b是 a的情况下&str,因为None => ""是 a&str并且Some(name) => name是 a&String所以它可以变成 a &str

  2. 如果 to 的参数unwrap_or是 a &str,而不是键入ba ,它会看到(即)中保存的类型和 to 的参数类型&str之间存在差异。a&Stringunwrap_or

这两种使类型推导以这种方式工作的情况有什么区别?

是否被unwrap_or实现为接受与选项包装的类型完全相同的类型,而不是只接受它放置在 match 中的通用值?

此外,在这种情况下,有什么方法可以完成unwrap_or工作,而不必String在外部范围内声明 a 或更改选项包装的类型?

我在其他地方得到的一个让它们工作的答案是:

与 相同的效果match,比match关于类型发生的事情更短,更明确。