如何返回 std::result<(), E> 的 Ok 单位类型? [英] How to return Ok unit type of std::result<(), E>?

查看:37
本文介绍了如何返回 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&lt;(), E&gt; 的 Ok 单位类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆