rust-by-practice/zh-CN/src/basic-types/functions.md

94 lines
1.6 KiB
Markdown
Raw Normal View History

2022-02-25 01:41:07 -06:00
# 函数
2022-03-02 08:11:56 -06:00
1. 🌟🌟🌟
2022-02-25 01:41:07 -06:00
```rust,editable
fn main() {
// 不要修改下面两行代码!
let (x, y) = (1, 2);
let s = sum(x, y);
2022-02-25 01:41:07 -06:00
assert_eq!(s, 3);
}
fn sum(x, y: i32) {
x + y;
}
```
2022-03-02 08:11:56 -06:00
2. 🌟🌟
2022-02-25 01:41:07 -06:00
```rust,editable
fn main() {
print();
}
// 使用另一个类型来替代 i32
fn print() -> i32 {
println!("hello,world");
}
```
2022-03-02 08:11:56 -06:00
3. 🌟🌟🌟
2022-02-25 01:41:07 -06:00
```rust,editable
// 用两种方法求解
2022-02-25 01:41:07 -06:00
fn main() {
never_return();
}
fn never_return() -> ! {
// 实现这个函数,不要修改函数签名!
}
2022-03-01 08:06:38 -06:00
```
2022-03-10 07:59:45 -06:00
4. 🌟🌟 发散函数( Diverging function )不会返回任何值,因此它们可以用于替代需要返回任何值的地方
```rust,editable
fn main() {
println!("Success!");
}
fn get_option(tp: u8) -> Option<i32> {
match tp {
1 => {
// TODO
}
_ => {
// TODO
}
};
// 这里与其返回一个 None不如使用发散函数替代
never_return_fn()
}
// 使用三种方法实现以下发散函数
fn never_return_fn() -> ! {
}
```
5. 🌟🌟
```rust,editable
fn main() {
// 填空
let b = __;
2022-07-20 10:37:58 -05:00
let _v = match b {
2022-03-10 07:59:45 -06:00
true => 1,
// 发散函数也可以用于 `match` 表达式,用于替代任何类型的值
false => {
println!("Success!");
panic!("we have no value for `false`, but we can panic")
}
};
2022-07-12 08:59:42 -05:00
println!("Exercise Failed if printing out this line!");
2022-03-10 07:59:45 -06:00
}
```
2022-05-13 12:17:08 -05:00
> 你可以在[这里](https://github.com/sunface/rust-by-practice/blob/master/solutions/basic-types/functions.md)找到答案(在 solutions 路径下)