字符、布尔、单元类型

字符

  1. 🌟
1. // 修改2处 `assert_eq!` 让代码工作

3. use std::mem::size_of_val;
4. fn main() {
5. let c1 = 'a';
6. assert_eq!(size_of_val(&c1),1);

8. let c2 = '中';
9. assert_eq!(size_of_val(&c2),3);

11. println!("Success!")
12. }
  1. 🌟
1. // 修改一行让代码正常打印
2. fn main() {
3. let c1 = "中";
4. print_char(c1);
5. }

7. fn print_char(c : char) {
8. println!("{}", c);
9. }

布尔

  1. 🌟
2. // 使成功打印
3. fn main() {
4. let _f: bool = false;

6. let t = true;
7. if !t {
8. println!("Success!")
9. }
10. }
  1. 🌟
2. fn main() {
3. let f = true;
4. let t = true && false;
5. assert_eq!(t, f);

7. println!("Success!")
8. }

单元类型

  1. 🌟🌟
2. // 让代码工作,但不要修改 `implicitly_ret_unit` !
3. fn main() {
4. let _v: () = ();

6. let v = (2, 3);
7. assert_eq!(v, implicitly_ret_unit());

9. println!("Success!")
10. }

12. fn implicitly_ret_unit() {
13. println!("I will return a ()")
14. }

16. // 不要使用下面的函数,它只用于演示!
17. fn explicitly_ret_unit() -> () {
18. println!("I will return a ()")
19. }
  1. 🌟🌟 单元类型占用的内存大小是多少?
2. // 让代码工作:修改 `assert!` 中的 `4`
3. use std::mem::size_of_val;
4. fn main() {
5. let unit: () = ();
6. assert!(size_of_val(&unit) == 4);

8. println!("Success!")
9. }

你可以在这里找到答案(在 solutions 路径下)