Rust Book Day 2 Flashcards

Learn Rust

1
Q

what are constants as opposed to immutable variables (7 facts)

A
  • are valid for the entire time a program runs (within the scope declared)
  • mut not allowed
  • use const keyword instead of let
  • type of the value must be annotated
  • can also be declared in global scope
  • may be set only to a constant expression, not the result of a value that could only be computed at runtime
  • capital letters and underscores

const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;

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

What is shadowing

A

declare a new variabled with the same name as a previous variable

fn main() {
let x = 5;

let x = x + 1;

{
    let x = x * 2;
    println!("The value of x in the inner scope is: {x}");
}

println!("The value of x is: {x}"); }

The value of x in the inner scope is: 12
The value of x is: 6 !!!!!

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

Can we shadow a variable with different type ?

A

let spaces = “ “;
let spaces = spaces.len();

This is fine even with a different type

let mut spaces = “ “;
spaces = spaces.len();

This is not fine (not shadowing actually)

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

what is scalar vs compound

A

scalar type represents a single value
- integers
- floating point numbers
- booleans
- characters

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

what are signed integers vs unsigned

A

unsigned can’t be negative

if you use u8 you can go from 0 to 255 where as i8 goes from -128 to 127

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

how to make 10000 easier to read

A

10_000

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

will 255 + 1 wrap when using u8 ?

A

yes, in –release mode, but not in standard debug mode,

you can use Wrapping(255u8) to make it specific

or wrapping_add method

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

what are floating point types and are they signed ?

A

signed, f32 and f64 (default, roughly same CPU speed)

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

what is result of
let truncated = -5 / 3;

A

// Results in -1

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

how is a boolean specified ?

A

bool

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

how is a character annotated ?

A

char

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
12
Q

how to define a tuple and name one fact about them

A

let my_tup: (i32, f64, u8) = (500, 6.4, 1)

once declared they cannot grow or shrink in size

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

how to destructure a tuple, name also an alternative “destructuring”

A

let tup = (500, 6.4, 1);

let (x, y, z) = tup;

println!("The value of y is: {y}");

alternatively:

let five_hundred = x.0;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what’s special about arrays in rust?

A

have fixed length, define it like this:
let a: [i32; 5] = [1, 2, 3, 4, 5];

let a = [3; 5];
// same as
let a = [3, 3, 3, 3, 3];

access it with a[0]

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

what are statements and expressions

A

statements are instructions which do not return a value, like let y = 6;

expressions evaluate to a resultant value

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

A new scope block created with curly brackets is an expression, for example:

A

let y = {
let x = 3;
x + 1
};

println!("The value of y is: {y}");

// returns 4

// note that there is no semicolon at the end

17
Q

how to specify return type of a function ?

A

->

like this:

fn plus_one(x: i32) -> i32 {
x + 1
}

18
Q

how to make comments?

A

//

19
Q

how to write if, elseif else if ?

A
  • no brackets needed for the condition
  • curly brackets needed for the code to execute

let number = 6;

if number % 4 == 0 {
    println!("number is divisible by 4");
} else if number % 3 == 0 {
    println!("number is divisible by 3");
} else if number % 2 == 0 {
    println!("number is divisible by 2");
} else {
    println!("number is not divisible by 4, 3, or 2");
}
20
Q

loop labels

A

// to break them
‘counting_up: loop {

break ‘counting_up;

21
Q

while

A

while number != 0 {
println!(“{number}!”);

    number -= 1;
}
22
Q

increase integer shorthand

A

index += 1;

23
Q

for loop

A

for element in a {
println!(“value is {element}”);
}

24
Q

countdown

A

for number in (1..4).rev() {
println!(“{number}!”);
}
println!(“LIFTOFF!!!”);

25
Q
A