mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 20:49:41 +00:00
add solutions for basic-types, fix errors
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
# Char, Bool and Unit
|
||||
|
||||
### Char
|
||||
🌟
|
||||
1. 🌟
|
||||
```rust, editable
|
||||
|
||||
// make it work
|
||||
@ -15,7 +15,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
🌟
|
||||
2. 🌟
|
||||
```rust, editable
|
||||
|
||||
// make it work
|
||||
@ -30,7 +30,7 @@ fn print_char(c : char) {
|
||||
```
|
||||
|
||||
### Bool
|
||||
🌟
|
||||
3. 🌟
|
||||
```rust, editable
|
||||
|
||||
// make the println! work
|
||||
@ -44,7 +44,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
🌟
|
||||
4. 🌟
|
||||
```rust, editable
|
||||
|
||||
// make it work
|
||||
@ -57,7 +57,7 @@ fn main() {
|
||||
|
||||
|
||||
### Unit type
|
||||
🌟🌟
|
||||
5. 🌟🌟
|
||||
```rust,editable
|
||||
|
||||
// make it work, don't modify `implicitly_ret_unit` !
|
||||
@ -78,7 +78,7 @@ fn explicitly_ret_unit() -> () {
|
||||
}
|
||||
```
|
||||
|
||||
🌟🌟 what's the size of the unit type?
|
||||
6. 🌟🌟 what's the size of the unit type?
|
||||
```rust,editable
|
||||
|
||||
// modify `4` in assert to make it work
|
||||
|
@ -1,11 +1,11 @@
|
||||
# Functions
|
||||
🌟🌟🌟
|
||||
1. 🌟🌟🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
// don't modify the following two lines!
|
||||
let (x, y) = (1, 2);
|
||||
let s = sum(1, 2);
|
||||
let s = sum(x, y);
|
||||
|
||||
assert_eq!(s, 3);
|
||||
}
|
||||
@ -16,7 +16,7 @@ fn sum(x, y: i32) {
|
||||
```
|
||||
|
||||
|
||||
🌟🌟
|
||||
2. 🌟
|
||||
```rust,editable
|
||||
fn main() {
|
||||
print();
|
||||
@ -29,15 +29,16 @@ fn print() -> i32 {
|
||||
```
|
||||
|
||||
|
||||
🌟🌟
|
||||
3. 🌟🌟🌟
|
||||
|
||||
```rust,editable
|
||||
// solve it in two ways
|
||||
fn main() {
|
||||
never_return();
|
||||
}
|
||||
|
||||
fn never_return() -> ! {
|
||||
// implement this function, don't modify fn signatures
|
||||
// implement this function, don't modify the fn signatures
|
||||
|
||||
}
|
||||
```
|
||||
|
@ -25,9 +25,9 @@ fn main() {
|
||||
```
|
||||
|
||||
### Exercises
|
||||
🌟🌟
|
||||
1. 🌟🌟
|
||||
```rust,editable
|
||||
// make it work with two ways: both modify the inner {}
|
||||
// make it work with two ways
|
||||
fn main() {
|
||||
let v = {
|
||||
let mut x = 1;
|
||||
@ -38,7 +38,7 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
🌟
|
||||
2. 🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {
|
||||
@ -48,10 +48,13 @@ fn main() {
|
||||
}
|
||||
```
|
||||
|
||||
🌟
|
||||
3. 🌟
|
||||
```rust,editable
|
||||
|
||||
fn main() {}
|
||||
fn main() {
|
||||
let s = sum(1 , 2);
|
||||
assert_eq!(s, 3);
|
||||
}
|
||||
|
||||
fn sum(x: i32, y: i32) -> i32 {
|
||||
x + y;
|
||||
|
Reference in New Issue
Block a user