Char, Bool and Unit

Char

  1. 🌟
2. // Make it work
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. 🌟
2. // Make it work
3. fn main() {
4. let c1 = "中";
5. print_char(c1);
6. }

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

Bool

  1. 🌟
2. // Make println! work
3. fn main() {
4. let _f: bool = false;

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

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

Unit type

  1. 🌟🌟
2. // Make it work, don't modify `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. // Don't use this one
17. fn explicitly_ret_unit() -> () {
18. println!("I will return a ()");
19. }
  1. 🌟🌟 What’s the size of the unit type?
2. // Modify `4` in assert to make it work
3. use std::mem::size_of_val;
4. fn main() {
5. let unit: () = ();
6. assert!(size_of_val(&unit) == 4);

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

You can find the solutions here(under the solutions path), but only use it when you need it