Variables & Mutability & Data Types Flashcards

(25 cards)

1
Q

What are the four primary scalar types?

A

integers, floating point numbers, Booleans, and characters

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

What are the built in integer types in Rust?

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

What do signed and unsigned refer to?

A

Signed and Unsigned refer to whether it’s possible for the number to negative- in other words, whether the number needs to have a sign with it (signed) or whether it will only ever be positive and can therefore be represented without a sign (unsigned).

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

How are signed numbers stored?

A

Signed numbers are stored using two’s complement representation

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

Whats the range of numbers that signed variant can store?

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

What do integer types default to in Rust?

A

integer types default to i32

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

What’s the primary situation in which you’d use isize or usize?

A

When indexing some sort of collection

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

What are the different ways of writing integer literals in Rust?

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

How does the mode in which you compile i.e., Debug or Release impact Integer overflow?

A

When compiling in debug mode, Rust includes checks for integer overflow that causes the program to panic at runtime. When compiling in release mode with the –release flag, Rust does not include checks for integer overflow that cause pancis. Instead, if overflow occurs, Rust perform’s two’s complement wrapping. Values greater than the maximum value the type can hold “wrap around” to the minimuim of the values the type can hold.

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

What are the families of methods provided by the standard library to explicitly handle the possibility of overflow?

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

What are Rusts two primative floating point types?

A

f32 and f64 and all floating point types are signed

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

What is the default floating point type in Rust and why?

A

f64 since on modern CPUs, it’s roughly the same speed as f32 but is capable of more precision.

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

What standard is used to represent floating point numbers?

A

IEEE-754

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

How does integer division work in Rust?

A

In Rust, integer division truncates toward zero to the nearest integer

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

What is the size of a boolean in Rust?

A

one byte

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

What is Rusts most primative alphabetic type?

A

char and unlike string literals they are specified with single quotes ‘ ‘

17
Q

How many bytes in size is Rust char type?

A

Rust char type is 4 bytes in size and represents a Unicode scalar value, which means it can represent alot more than just ASCII

18
Q

What is a compound type and what are Rusts two primitive compound types?

A

Compound types can group multiple values into one type. Rust’s two primitive compound types anre tuples and arrays

19
Q

What is a tuple in Rust?

A

A way of grouping together a number of values with a variety of types into one compound type. Tuples have a fixed length. They cannot grow or shrink in size once they are declared

let tup: (i32, f64, u8) = (500, 6.4, 1);
let (x, y, z) = tup; // we are doing what's called destructing here. destructing breaks the single tuple into three parts
println!("The value of y is: {y}");
let five_hundred = x.0; // another way of accessing the tuple element using the (.) followed by the index 
20
Q

What is a tuple without any value called?

A

unit and is written () and represents an empty value or an empty return type. Expressions imlicitly return the unit value if they don’t return any other value

21
Q

Can the elements of an array be of different types?

A

No. Unlike a tuple, every element of an array must have the same type.

22
Q

Are Arrays in Rust fixed in length?

23
Q

How would you write an array in Rust?

A
let a = [1, 2, 3, 4, 5];
let a: [i32; 5] = [1, 2, 3, 4, 5] // The type of the array written and the number of elements
let a = [3; 5]; // Initializes an array to contain the same value for each element by specifying the initial value, followed by a semicolon, and the length of the array in square brackets. So  [3, 3, 3, 3, 3]
24
Q

Why are arrays useful?

A

Arrays are useful when you want the data allocated on the stack rather than the heap or when you want to ensure you always have a fixed number of elements.

25
How do you access elements of an array?
An array is a single chunk of memory of a known, fixed size that can be allocated on the stack let a = [1, 2, 3, 4, 5]; let first = a[0]; let second = a[1];