Skip to main content

3.2 Char, Bool and Unit

About 3 minRustcrashcoursefreecodecampzubiarfanyoutuberustrswasmassembly

3.2 Char, Bool and Unit κ΄€λ ¨

Rust By Practice

Rust By Practice

4.2 Char, Bool and Unit | Rust By Practice
4.2 Char, Bool and Unit

Char

1. 🌟

πŸ’€Problem
// Make it work
use std::mem::size_of_val;
fn main() {
    let c1 = 'a';
    assert_eq!(size_of_val(&c1),1); 

    let c2 = 'δΈ­';
    assert_eq!(size_of_val(&c2),3); 

    println!("Success!");
}
//
//
/*
   Compiling playground v0.0.1 (/playground)
      Finished dev [unoptimized + debuginfo] target(s) in 0.59s
       Running `target/debug/playground`
  thread 'main' panicked at 'assertion failed: `(left == right)`
    left: `4`,
   right: `1`', src/main.rs:6:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace  
*/

2. 🌟

πŸ’€Problem
// Make it work
fn main() {
    let c1 = "δΈ­";
    print_char(c1);
} 

fn print_char(c : char) {
    println!("{}", c);
}
//
//
/*
     Compiling playground v0.0.1 (/playground)
  error[E0308]: mismatched types
   --> src/main.rs:5:16
    |
  5 |     print_char(c1);
    |     ---------- ^^ expected `char`, found `&str`
    |     |
    |     arguments to this function are incorrect
    |
  note: function defined here
   --> src/main.rs:8:4
    |
  8 | fn print_char(c : char) {
    |    ^^^^^^^^^^ --------
  
  For more information about this error, try `rustc --explain E0308`.
  error: could not compile `playground` (bin "playground") due to previous error
*/

Bool

3. 🌟

πŸ’€Problem
// Make println! work
fn main() {
    let _f: bool = false;

    let t = true;
    if !t {
        println!("Success!");
    }
}
//
//
/*
*/

4. 🌟

πŸ’€Problem
// Make it work
fn main() {
    let f = true;
    let t = true && false;
    assert_eq!(t, f);

    println!("Success!");
}
//
//
/*
     Compiling playground v0.0.1 (/playground)
      Finished dev [unoptimized + debuginfo] target(s) in 0.54s
       Running `target/debug/playground`
  thread 'main' panicked at 'assertion failed: `(left == right)`
    left: `false`,
   right: `true`', src/main.rs:6:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
*/

Unit type

5. 🌟🌟

πŸ’€Problem
// Make it work, don't modify `implicitly_ret_unit` !
fn main() {
    let _v: () = ();

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

    println!("Success!");
}

fn implicitly_ret_unit() {
    println!("I will return a ()");
}

// Don't use this one
fn explicitly_ret_unit() -> () {
    println!("I will return a ()");
}
//
//
/*
     Compiling playground v0.0.1 (/playground)
  error[E0308]: mismatched types
   --> src/main.rs:7:5
    |
  7 |     assert_eq!(v, implicitly_ret_unit());
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |     |
    |     expected `({integer}, {integer})`, found `()`
    |     expected because this is `({integer}, {integer})`
    |
    = note:  expected tuple `({integer}, {integer})`
            found unit type `()`
    = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
  
  For more information about this error, try `rustc --explain E0308`.
  error: could not compile `playground` (bin "playground") due to previous error
*/

6. 🌟🌟 What's the size of the unit type?

πŸ’€Problem
// Modify `4` in assert to make it work
use std::mem::size_of_val;
fn main() {
    let unit: () = ();
    assert!(size_of_val(&unit) == 4);

    println!("Success!");
}
//
//
/*
     Compiling playground v0.0.1 (/playground)
      Finished dev [unoptimized + debuginfo] target(s) in 0.71s
       Running `target/debug/playground`
  thread 'main' panicked at 'assertion failed: size_of_val(&unit) == 4', src/main.rs:6:5
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
*/

이찬희 (MarkiiimarK)
Never Stop Learning.