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

46 lines
690 B
Markdown
Raw Normal View History

# Functions
1. 🌟🌟🌟
2022-02-25 01:41:07 -06:00
```rust,editable
fn main() {
// don't modify the following two lines!
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;
}
```
2. 🌟
2022-02-25 01:41:07 -06:00
```rust,editable
fn main() {
print();
}
// replace i32 with another type
fn print() -> i32 {
println!("hello,world");
}
```
3. 🌟🌟🌟
2022-02-25 01:41:07 -06:00
```rust,editable
// solve it in two ways
2022-02-25 01:41:07 -06:00
fn main() {
never_return();
}
fn never_return() -> ! {
// implement this function, don't modify the fn signatures
2022-02-25 01:41:07 -06:00
}
2022-03-01 08:06:38 -06:00
```
> 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