diff --git a/solutions/basic-types/functions.md b/solutions/basic-types/functions.md index 770c93f..04d315a 100644 --- a/solutions/basic-types/functions.md +++ b/solutions/basic-types/functions.md @@ -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 { + 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!"); +} + ``` \ No newline at end of file diff --git a/src/basic-types/functions.md b/src/basic-types/functions.md index e3cb022..f6adb06 100644 --- a/src/basic-types/functions.md +++ b/src/basic-types/functions.md @@ -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 { + 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 \ No newline at end of file diff --git a/zh-CN/src/basic-types/functions.md b/zh-CN/src/basic-types/functions.md index b7886c9..38d339c 100644 --- a/zh-CN/src/basic-types/functions.md +++ b/zh-CN/src/basic-types/functions.md @@ -43,4 +43,51 @@ fn never_return() -> ! { } ``` +4. 🌟🌟 发散函数( Diverging function )不会返回任何值,因此它们可以用于替代需要返回任何值的地方 +```rust,editable + +fn main() { + println!("Success!"); +} + +fn get_option(tp: u8) -> Option { + match tp { + 1 => { + // TODO + } + _ => { + // TODO + } + }; + + // 这里与其返回一个 None,不如使用发散函数替代 + never_return_fn() +} + +// 使用三种方法实现以下发散函数 +fn never_return_fn() -> ! { + +} +``` + +5. 🌟🌟 +```rust,editable + +fn main() { + // 填空 + let b = __; + + let v = match b { + true => 1, + // 发散函数也可以用于 `match` 表达式,用于替代任何类型的值 + false => { + println!("Success!"); + panic!("we have no value for `false`, but we can panic") + } + }; + + println!("Excercise Failed if printing out this line!"); +} +``` + > 你可以在[这里](https://github.com/sunface/rust-by-practice)找到答案(在 solutions 路径下) \ No newline at end of file