gpt4 book ai didi

loops - 当我只关心一个变体而不关心变体中的值时,循环匹配的惯用方法是什么?

转载 作者:行者123 更新时间:2023-12-03 11:25:19 24 4
gpt4 key购买 nike

loop {
match stream.write(&buffer) {
Ok(_) => break,
_ => {}
}
}
有没有办法以更具可读性/惯用的方式编写它?我不需要在 match 内做任何事情, 在 Ok 之后我不需要任何操作或在 Err 之后,因为每当返回 Ok 时,缓冲区都会更新为引用.
就像是:
while match some_fn() != Ok() {
...
}

最佳答案

一般情况由 ForceBru's answer 涵盖,但因为你有一个 Result并且不关心 Ok 里面的值,您可以使用 Result::is_ok (或 Result::is_err ):

while stream.write(&buffer).is_err() {}
正如 rodrigo in the comments 所指出的:

Worth noting that Rust 1.42 adds the matches! macro, so you can write while matches!(stream.write(&buffer), Ok(_)) {}, useful for other enums that lack the is_* functions.

关于loops - 当我只关心一个变体而不关心变体中的值时,循环匹配的惯用方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64451464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com