类型铸造选项中的生锈
如何在Rust中施放可选值?
这就是我想到的,它确实有效,但我认为必须有一种更优雅的方式。
pub fn option_t_to_i32_option<T1, T2>(optional_val: Option<T1>) -> Option<T2>
where
T2: From<T1>,
{
return match optional_val {
Some(val) => Some(T2::from(val)),
None => None,
};
}
How do I cast optional values in rust?
This is what I came up with, which does work, but I think there must be a more elegant way.
pub fn option_t_to_i32_option<T1, T2>(optional_val: Option<T1>) -> Option<T2>
where
T2: From<T1>,
{
return match optional_val {
Some(val) => Some(T2::from(val)),
None => None,
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是
map
in :: in :: in
您的约束:Playground> Playground
根据您的功能名称,也许您想实际上将输出类型与
i32
i325b4e041d1b41d1b4d0286b745835835cca1165宁愿使用
_。MAP(in :: in :: in ton)
无论您需要去option&lt; t&gt; =&gt;选项&lt; i32&gt;
。Just
map
Into::into
for your constrains:Playground
As per your function name, maybe you would like to actually match the output type to
i32
:Playground
Btw, since this is a wrapper, you could rather use
_.map(Into::into)
wherever you need to goOption<T> => Option<i32>
instead.