rust - 在 Rust 中 Scala 的 getOrElse 是什么?

标签 rust option-type

在 Scala 中,我可以使用 getOrElse 作为从 Option 获取默认值的便捷方式,在 Rust 中这等同于什么?

val threeOpt = Some(3)
val shouldBeThree = threeOpt.getOrElse(-1) // got 3

最佳答案

您可以使用 unwrap_orunwrap_or_else

  • unwrap_or 急切求值
  • unwrap_or_else 被延迟计算

See here for differences between eager and lazy

关于rust - 在 Rust 中 Scala 的 getOrElse 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58693667/

相关文章:

java - 如何使用 java 8 处理可空列表?

java - 用Optional#ifPresent替换空检查的好处

rust - 无法为返回引用的闭包推断适当的生存期

rust - 为什么 `get` 对 std::vec::Vec 和 &std::vec::Vec 都有效?

java - 在控制层编写决策语句

swift - 可选的 UInt8?给出 2 字节内存大小

objective-c - swift 1.2 : Implement optional property from Objc protocol

rust 和紫 Crystal - 错误 [E0433] : failed to resolve: could not find `__rt` in `quote`

c++ - Rust 找不到 Microsoft C++ 构建工具

rust - 如何反转十六进制字符串?