add solutions for basic-types, fix errors

This commit is contained in:
sunface 2022-03-02 16:27:16 +08:00
parent 6be05a6686
commit 59851ca1bb
9 changed files with 211 additions and 26 deletions

View File

@ -0,0 +1,75 @@
1.
```rust
use std::mem::size_of_val;
fn main() {
let c1 = 'a';
assert_eq!(size_of_val(&c1),4);
let c2 = '中';
assert_eq!(size_of_val(&c2),4);
}
```
2.
```rust
fn main() {
let c1 = '中';
print_char(c1);
}
fn print_char(c : char) {
println!("{}", c);
}
```
3.
```rust
fn main() {
let _f: bool = false;
let t = false;
if !t {
println!("hello, world");
}
}
```
4.
```rust
fn main() {
let f = true;
let t = true || false;
assert_eq!(t, f);
}
```
5.
```rust
fn main() {
let v0: () = ();
let v = (2, 3);
assert_eq!(v0, implicitly_ret_unit())
}
fn implicitly_ret_unit() {
println!("I will returen a ()")
}
// don't use this one
fn explicitly_ret_unit() -> () {
println!("I will returen a ()")
}
```
6.
```rust
use std::mem::size_of_val;
fn main() {
let unit: () = ();
// unit type does't occupy any memeory space
assert!(size_of_val(&unit) == 0);
}
```

View File

@ -0,0 +1,55 @@
1.
```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.
```rust
fn main() {
print();
}
// replace i32 with another type
fn print() -> () {
println!("hello,world");
}
```
3.
```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;
fn never_return() -> ! {
// implement this function, don't modify fn signatures
loop {
println!("I return nothing");
// sleeping for 1 second to avoid exhausting the cpu resoucre
thread::sleep(time::Duration::from_secs(1))
}
}
```

View File

@ -0,0 +1,47 @@
1.
```rust
fn main() {
let v = {
let mut x = 1;
x += 2
};
assert_eq!(v, ());
}
```
```rust
fn main() {
let v = {
let mut x = 1;
x += 2;
x
};
assert_eq!(v, 3);
}
```
2.
```rust
fn main() {
let v = {
let x = 3;
x
};
assert!(v == 3);
}
```
3.
```rust
fn main() {
let s = sum(1 , 2);
assert_eq!(s, 3);
}
fn sum(x: i32, y: i32) -> i32 {
x + y
}
```

View File

@ -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

View File

@ -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
}
```

View File

@ -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;

View File

@ -1,7 +1,7 @@
# 字符、布尔、单元类型
### 字符
🌟
1. 🌟
```rust, editable
use std::mem::size_of_val;
@ -14,7 +14,7 @@ fn main() {
}
```
🌟
2. 🌟
```rust, editable
fn main() {
@ -28,7 +28,7 @@ fn print_char(c : char) {
```
### 布尔
🌟
3. 🌟
```rust, editable
// 让 println! 工作
@ -42,7 +42,7 @@ fn main() {
}
```
🌟
4. 🌟
```rust, editable
fn main() {
@ -54,7 +54,7 @@ fn main() {
### 单元类型
🌟🌟
5. 🌟🌟
```rust,editable
// 让代码工作,但不要修改 `implicitly_ret_unit` !
@ -75,7 +75,7 @@ fn explicitly_ret_unit() -> () {
}
```
🌟🌟 单元类型占用的内存大小是多少?
6. 🌟🌟 单元类型占用的内存大小是多少?
```rust,editable
// 让代码工作:修改 `assert!` 中的 `4`

View File

@ -5,7 +5,7 @@
fn main() {
// 不要修改下面两行代码!
let (x, y) = (1, 2);
let s = sum(1, 2);
let s = sum(x, y);
assert_eq!(s, 3);
}
@ -29,9 +29,10 @@ fn print() -> i32 {
```
🌟🌟
🌟🌟🌟
```rust,editable
// 用两种方法求解
fn main() {
never_return();
}

View File

@ -27,7 +27,7 @@ fn main() {
### 练习
🌟🌟
```rust,editable
// 使用两种方法修改内部的 {} 中的内容
// 使用两种方法让代码工作起来
fn main() {
let v = {
let mut x = 1;
@ -51,7 +51,10 @@ fn main() {
🌟
```rust,editable
fn main() {}
fn main() {
let s = sum(1 , 2);
assert_eq!(s, 3);
}
fn sum(x: i32, y: i32) -> i32 {
x + y;