【发布时间】:2020-07-09 09:00:48
【问题描述】:
我是 Python 的 Rust 新手。我认为这是一个基本问题,但我太新了,无法通过 Type Casting Option 等关键字找到答案。
在 Python 中,为了让类型检查器知道返回类型不是 Optional[int] + int,我们可以处理 assert 逻辑来强制类型检查器知道 x 在行 assert 之后永远不会是 None。
from typing import Optional
def add_one(x: Optional[int] = None) -> int:
if x is None:
x = 0
assert x is not None
return x + 1
if __name__ == '__main__':
add_one(0) # 1
add_one() # 1
add_one(999) # 1000
在Rust中,假设接口相同,那么如何实现相同的东西?即如何让编译器知道x的类型不再是Option?
fn add_one(mut x: Option<i32>) -> i32 {
if x == None {
x = Some(0);
}
return x + 1;
}
fn main() {
add_one(Some(0));
add_one(None);
add_one(Some(999));
}
这是错误信息:
error[E0369]: binary operation `+` cannot be applied to type `std::option::Option<i32>`
--> tmp.rs:5:14
|
5 | return x + 1;
| - ^ - {integer}
| |
| std::option::Option<i32>
|
= note: an implementation of `std::ops::Add` might be missing for `std::option::Option<i32>`
请注意,我尝试过添加另一个类型为 i32 (let y: i32 = x;) 的变量,但对于以下消息也不起作用。
error[E0308]: mismatched types
--> tmp.rs:5:22
|
5 | let y: i32 = x;
| ^ expected i32, found enum `std::option::Option`
|
= note: expected type `i32`
found type `std::option::Option<i32>`
【问题讨论】:
-
解决方案在答案中,但如果您想详细了解 rust 如何处理
Option,您可以查看此帖子:stackoverflow.com/questions/53901550/…
标签: rust casting type-conversion