mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 12:39:42 +00:00
add exercises for diverging functions
This commit is contained in:
@ -52,4 +52,66 @@ fn never_return() -> ! {
|
||||
thread::sleep(time::Duration::from_secs(1))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
4.
|
||||
```rust
|
||||
fn main() {
|
||||
println!("Success!");
|
||||
}
|
||||
|
||||
fn get_option(tp: u8) -> Option<i32> {
|
||||
match tp {
|
||||
1 => {
|
||||
// TODO
|
||||
}
|
||||
_ => {
|
||||
// TODO
|
||||
}
|
||||
};
|
||||
|
||||
never_return_fn()
|
||||
}
|
||||
|
||||
// IMPLEMENT this function
|
||||
// DON'T change any code else
|
||||
fn never_return_fn() -> ! {
|
||||
unimplemented!()
|
||||
}
|
||||
```
|
||||
|
||||
```rust
|
||||
// IMPLEMENT this function in THREE ways
|
||||
fn never_return_fn() -> ! {
|
||||
panic!()
|
||||
}
|
||||
```
|
||||
|
||||
```rust
|
||||
// IMPLEMENT this function in THREE ways
|
||||
fn never_return_fn() -> ! {
|
||||
loop {
|
||||
std::thread::sleep(std::time::Duration::from_secs(1))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
5.
|
||||
```rust
|
||||
fn main() {
|
||||
// FILL in the blank
|
||||
let b = false;
|
||||
|
||||
let v = match b {
|
||||
true => 1,
|
||||
// Diverging functions can also be used in match expression
|
||||
false => {
|
||||
println!("Success!");
|
||||
panic!("we have no value for `false`, but we can panic")
|
||||
}
|
||||
};
|
||||
|
||||
println!("Excercise Failed if printing out this line!");
|
||||
}
|
||||
|
||||
```
|
Reference in New Issue
Block a user