Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent of Scala's getOrElse in Rust?

In Scala, I can use getOrElse as a convenient way to get a default value from Option, what's the equivalent of this in Rust?

val threeOpt = Some(3)
val shouldBeThree = threeOpt.getOrElse(-1) // got 3
like image 792
Renkai Avatar asked Nov 07 '25 10:11

Renkai


1 Answers

You can use either unwrap_or or unwrap_or_else

  • unwrap_or is eagerly evaluated
  • unwrap_or_else is lazily evaluated

See here for differences between eager and lazy

like image 105
Shankar Shastri Avatar answered Nov 09 '25 09:11

Shankar Shastri