R3: Data Types Flashcards

1
Q

Integer Types

A
let guess: u32 = "42"

Length Signed Unsigned
8-bit i8 u8
16-bit i16 u16
32-bit i32 u32
64-bit i64 u64
128-bit i128 u128
arch isize usize

Number literals Example
Decimal 98_222
Hex 0xff
Octal 0o77
Binary 0b1111_0000
Byte (u8 only) b’A’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Floating-Point Types

A
    let x = 2.0; // f64
    let y: f32 = 3.0; // f32
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Numeric Operations

A
    // addition
    let sum = 5 + 10;

    // subtraction
    let difference = 95.5 - 4.3;

    // multiplication
    let product = 4 * 30;

    // division
    let quotient = 56.7 / 32.2;
    let truncated = -5 / 3; // Results in -1

    // remainder
    let remainder = 43 % 5;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The Boolean Type

A
fn main() {
    let t = true;

    let f: bool = false; // with explicit type annotation
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The Character Type

A
fn main() {
    let c = 'z';
    let z: char = 'ℤ'; // with explicit type annotation
    let heart_eyed_cat = '😻';
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The Tuple Type

A

Tuples have a fixed length: once declared, they cannot grow or shrink in size.
~~~
fn main() {
let tup = (500, 6.4, 1);

let (x, y, z) = tup;

println!("The value of y is: {y}"); } ~~~
fn main() {
    let x: (i32, f64, u8) = (500, 6.4, 1);

    let five_hundred = x.0;

    let six_point_four = x.1;

    let one = x.2;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The Array Type

A

Unlike a tuple, every element of an array must have the same type. Unlike arrays in some other languages, arrays in Rust have a fixed length.

fn main() {
    let a1 = [1, 2, 3, 4, 5];
    let a2: [i32; 5] = [1, 2, 3, 4, 5];
    let a3 = [3; 5];

    let first = a1[0];
    let second = a1[1];
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

String

A
  • String literals are convenient bc they’re immutable
  • manages data allocated on the heap and as such is able to store an amount of text that is unknown to us at compile time.
  • This kind of string can be mutated:
  • Example: l
    let mut s = String::from("hello");

    s.push_str(", world!"); // push_str() appends a literal to a String

    println!("{}", s); // This will print `hello, world!`
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

String

A
  • String literals are convenient bc they’re immutable
  • manages data allocated on the heap and as such is able to store an amount of text that is unknown to us at compile time.
  • This kind of string can be mutated:
  • Example: l
    let mut s = String::from("hello");

    s.push_str(", world!"); // push_str() appends a literal to a String

    println!("{}", s); // This will print `hello, world!`
How well did you know this?
1
Not at all
2
3
4
5
Perfectly