两个选项之间的 Rust OR
Rust OR between two options
我想知道是否可以在两个选项之间执行或操作。像 :
let a = Some(3);
let b = None;
let result = a || b;
这里我希望 result
变量具有 a
的值(如果它是 Some
或 b
.
的值
我在 Internet 上没有找到这方面的任何文档。
在 Rust 中 逻辑或 (||
) 的操作数必须是 bool
。所以它不可能与 Option
或任何其他类型一起使用。
但请参阅Option
的documentation中的or
方法。
Returns the option if it contains a value, otherwise returns optb.
你的问题可以这样解决:
let a = Some(3);
let b = None;
let result = a.or(b); // result: Some(3)
如果您希望结果为 bool
,您可以使用 is_some
或 is_none
let result = a.or(b).is_some(); // true
let result = None.or(None).is_some(); // false
OR_ELSE
Arguments passed to or are eagerly evaluated; if you are passing the
result of a function call, it is recommended to use or_else, which is
lazily evaluated.
您也可以在这种情况下使用 or_else
:
let a = Some(3);
let result = a.or(produce_option());
从代码中可以看出,无论你的a
是Some
还是None
,它都会运行produce_option
,这可能效率不高一些(或许多)情况。
let result = a.or_else(produce_option);
这里produce_option
只会在a
为None
时调用
AND
对于逻辑与 (&&
),您可以使用 Option
的 and
method。
let a = None;
let b = Some(4);
let result = a.and(b); // result: None
对于惰性评估,您可以像 OR_ELSE
一样使用 AND_THEN。
UNWRAP_OR
unwrap_or
或 unwrap_or_else
如果您想在比较时获取选项内的值,也可以使用。但这要视情况而定,我以这个为例:
let a = None;
let b = None;
let x:i32 = a.unwrap_or(b.unwrap_or_default()); //Since `i32`'s default value is 0 `x` will be equal to 0
let a = None;
let b = Some(3);
let x:i32 = a.unwrap_or_else(||b.unwrap_or_default()); // `x` will be eqaul to 3
//using `unwrap_or_else` might be a better idea, because of the lazy evaluation.
注意:这个答案集中在 Option
但这些都适用于具有 相同错误类型的 Result
对象.
我想知道是否可以在两个选项之间执行或操作。像 :
let a = Some(3);
let b = None;
let result = a || b;
这里我希望 result
变量具有 a
的值(如果它是 Some
或 b
.
我在 Internet 上没有找到这方面的任何文档。
在 Rust 中 逻辑或 (||
) 的操作数必须是 bool
。所以它不可能与 Option
或任何其他类型一起使用。
但请参阅Option
的documentation中的or
方法。
Returns the option if it contains a value, otherwise returns optb.
你的问题可以这样解决:
let a = Some(3);
let b = None;
let result = a.or(b); // result: Some(3)
如果您希望结果为 bool
,您可以使用 is_some
或 is_none
let result = a.or(b).is_some(); // true
let result = None.or(None).is_some(); // false
OR_ELSE
Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use or_else, which is lazily evaluated.
您也可以在这种情况下使用 or_else
:
let a = Some(3);
let result = a.or(produce_option());
从代码中可以看出,无论你的a
是Some
还是None
,它都会运行produce_option
,这可能效率不高一些(或许多)情况。
let result = a.or_else(produce_option);
这里produce_option
只会在a
为None
AND
对于逻辑与 (&&
),您可以使用 Option
的 and
method。
let a = None;
let b = Some(4);
let result = a.and(b); // result: None
对于惰性评估,您可以像 OR_ELSE
一样使用 AND_THEN。
UNWRAP_OR
unwrap_or
或 unwrap_or_else
如果您想在比较时获取选项内的值,也可以使用。但这要视情况而定,我以这个为例:
let a = None;
let b = None;
let x:i32 = a.unwrap_or(b.unwrap_or_default()); //Since `i32`'s default value is 0 `x` will be equal to 0
let a = None;
let b = Some(3);
let x:i32 = a.unwrap_or_else(||b.unwrap_or_default()); // `x` will be eqaul to 3
//using `unwrap_or_else` might be a better idea, because of the lazy evaluation.
注意:这个答案集中在 Option
但这些都适用于具有 相同错误类型的 Result
对象.