|
|
|
|
|
by umanwizard
779 days ago
|
|
You can actually implement the C++ behavior, if you want: unsafe fn super_unwrap<T>(x: Option<T>) -> T {
match x {
Some(val) => val,
None => unreachable_unchecked!(),
}
}
But defaults matter, and Rust certainly doesn’t make this kind of thing ergonomic (which is a correct decision on the Rust designers’ part). |
|
Because all Rust's methods can be called as free functions, you can literally write Option::unwrap_unchecked for the same behaviour, or you can some_option.unwrap_unchecked() (in both cases you will need to be in unsafe context for this to be allowed and should write a SAFETY comment explaining why you're sure it's correct)