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

130 lines
1.9 KiB
Markdown
Raw Normal View History

1.
2022-05-01 00:08:50 -05:00
```rust
fn main() {
// don't modify the following two lines!
let (x, y) = (1, 2);
let s = sum(x, y);
assert_eq!(s, 3);
}
fn sum(x: i32, y: i32) -> i32 {
x + y
}
```
2.
2022-05-01 00:08:50 -05:00
```rust
fn main() {
print();
}
// replace i32 with another type
fn print() -> () {
println!("hello,world");
}
```
3.
2022-05-01 00:08:50 -05:00
```rust
fn main() {
never_return();
}
fn never_return() -> ! {
// implement this function, don't modify fn signatures
panic!("I return nothing!")
}
```
```rust
fn main() {
never_return();
}
use std::thread;
use std::time;
2022-05-01 00:08:50 -05:00
fn never_return() -> ! {
// implement this function, don't modify fn signatures
loop {
println!("I return nothing");
2022-07-12 08:59:42 -05:00
// sleeping for 1 second to avoid exhausting the cpu resource
thread::sleep(time::Duration::from_secs(1))
}
}
2022-03-10 07:59:45 -06:00
```
4.
2022-05-01 00:08:50 -05:00
2022-03-10 07:59:45 -06:00
```rust
fn main() {
println!("Success!");
}
fn get_option(tp: u8) -> Option<i32> {
match tp {
1 => {
// TODO
}
_ => {
// TODO
}
};
2022-05-01 00:08:50 -05:00
2022-03-10 07:59:45 -06:00
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() -> ! {
todo!();
}
```
2022-03-10 07:59:45 -06:00
```rust
// IMPLEMENT this function in THREE ways
fn never_return_fn() -> ! {
loop {
std::thread::sleep(std::time::Duration::from_secs(1))
}
}
```
5.
2022-05-01 00:08:50 -05:00
2022-03-10 07:59:45 -06:00
```rust
fn main() {
// FILL in the blank
let b = false;
2022-07-20 10:37:58 -05:00
let _v = match b {
2022-03-10 07:59:45 -06:00
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!("Exercise Failed if printing out this line!");
2022-03-10 07:59:45 -06:00
}
```