Rustlings Topic: Primitive Types

Have a look at Data Types & The Slice Type to learn about primitive types.

You may find solution code for the topic from my repo.

  1. primitive_types1.rs
  2. primitive_types2.rs
  3. primitive_types3.rs
  4. primitive_types4.rs
  5. primitive_types5.rs
  6. primitive_types6.rs

primitive_types1.rs

No comments are necessary this time.

/* file: "exercises/primitive_types/primitive_types1.rs" */
fn main() {
    // Booleans (`bool`)

    let is_morning = true;
    if is_morning {
        println!("Good morning!");
    }

    let is_evening = !is_morning;
    if is_evening {
        println!("Good evening!");
    }
}

primitive_types2.rs

No comments are necessary this time. * 2

/* file: "exercises/primitive_types/primitive_types2.rs" */
fn main() {
    // Characters (`char`)

    let my_first_initial = 'C';
    if my_first_initial.is_alphabetic() {
        println!("Alphabetical!");
    } else if my_first_initial.is_numeric() {
        println!("Numerical!");
    } else {
        println!("Neither alphabetic nor numeric!");
    }

    let your_character = '🍻';
    if your_character.is_alphabetic() {
        println!("Alphabetical!");
    } else if your_character.is_numeric() {
        println!("Numerical!");
    } else {
        println!("Neither alphabetic nor numeric!");
    }
}

primitive_types3.rs

Array is one of the common and useful data structure of the programming world.

A fixed-size array, denoted [T; N], for the element type, T, and the non-negative compile-time constant size, N.

Unfortunately, Rust doesn’t allow runtime deterministic array size. If you want one, easiest solution would be using Vec. But keep in mind that the vector is allocated at the heap.

/* file: "exercises/primitive_types/primitive_types3.rs" */
fn main() {
    let a = [0; 100];

    if a.len() >= 100 {
        println!("Wow, that's a big array!");
    } else {
        println!("Meh, I eat arrays like that for breakfast.");
    }
}

primitive_types4.rs

Slice is also very handy and quite often used. Especially for the function parameter type. So you better get used to it. Take a look at The Slice Type chapter and Primitive Type slice API document.

/* file: "exercises/primitive_types/primitive_types4.rs" */
#[test]
fn slice_out_of_array() {
    let a = [1, 2, 3, 4, 5];

    let nice_slice = &a[1..4];

    assert_eq!([2, 3, 4], nice_slice)
}

primitive_types5.rs

Tuple is also widely used (except for the C/C++ I guess…?) primitive type in programming world. Take a look at The Tuple Type chapter & Primitive Type tuple API document.

/* file: "exercises/primitive_types/primitive_types5.rs" */
fn main() {
    let cat = ("Furry McFurson", 3.5);
    let (name, age) = cat;

    println!("{} is {} years old.", name, age);
}

primitive_types6.rs

No comments are necessary this time. * 3

/* file: "exercises/primitive_types/primitive_types6.rs" */
#[test]
fn indexing_tuple() {
    let numbers = (1, 2, 3);
    // Replace below ??? with the tuple indexing syntax.
    let second = numbers.1;

    assert_eq!(2, second, "This is not the 2nd number in the tuple!")
}

Continue with Rustlings Solution