如何返回 std::result<(), E> 的 Ok 单位类型? [英] How to return Ok unit type of std::result<(), E>?
本文介绍了如何返回 std::result<(), E> 的 Ok 单位类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我定义一个函数:
fn f() -> Result<(), E> {
// How to return Ok()?
}
如何返回 std::result
中单元类型为 ()
的 Ok
?
How can I return the Ok
in std::result
with the unit type ()
?
推荐答案
()
类型的唯一值是 ()
,所以把它放在 好的
构造函数:
The only value of type ()
is ()
, so just put that inside the Ok
constructor:
fn f() -> Result<(), E> {
Ok(())
}
这篇关于如何返回 std::result<(), E> 的 Ok 单位类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文