mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-25 05:29:41 +00:00
add exercises for diverging functions
This commit is contained in:
@ -48,4 +48,54 @@ fn never_return() -> ! {
|
||||
}
|
||||
```
|
||||
|
||||
### Diverging functions
|
||||
Diverging functions never return to the caller, so they may be used in places where a value of any type is expected.
|
||||
|
||||
4. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
println!("Success!");
|
||||
}
|
||||
|
||||
fn get_option(tp: u8) -> Option<i32> {
|
||||
match tp {
|
||||
1 => {
|
||||
// TODO
|
||||
}
|
||||
_ => {
|
||||
// TODO
|
||||
}
|
||||
};
|
||||
|
||||
// Rather than returning a None, we use a diverging function instead
|
||||
never_return_fn()
|
||||
}
|
||||
|
||||
// IMPLEMENT this function in THREE ways
|
||||
fn never_return_fn() -> ! {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
5. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
// FILL in the blank
|
||||
let b = __;
|
||||
|
||||
let v = match b {
|
||||
true => 1,
|
||||
// Diverging functions can also be used in match expression to replace a value of any value
|
||||
false => {
|
||||
println!("Success!");
|
||||
panic!("we have no value for `false`, but we can panic")
|
||||
}
|
||||
};
|
||||
|
||||
println!("Excercise Failed if printing out this line!");
|
||||
}
|
||||
```
|
||||
|
||||
> You can find the solutions [here](https://github.com/sunface/rust-by-practice)(under the solutions path), but only use it when you need it
|
Reference in New Issue
Block a user