What are the four primary scalar types?
integers, floating point numbers, Booleans, and characters
What are the built in integer types in Rust?
What do signed and unsigned refer to?
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 are signed numbers stored?
Signed numbers are stored using two’s complement representation
Whats the range of numbers that signed variant can store?
What do integer types default to in Rust?
integer types default to i32
What’s the primary situation in which you’d use isize or usize?
When indexing some sort of collection
What are the different ways of writing integer literals in Rust?
How does the mode in which you compile i.e., Debug or Release impact Integer overflow?
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.
What are the families of methods provided by the standard library to explicitly handle the possibility of overflow?
What are Rusts two primative floating point types?
f32 and f64 and all floating point types are signed
What is the default floating point type in Rust and why?
f64 since on modern CPUs, it’s roughly the same speed as f32 but is capable of more precision.
What standard is used to represent floating point numbers?
IEEE-754
How does integer division work in Rust?
In Rust, integer division truncates toward zero to the nearest integer
What is the size of a boolean in Rust?
one byte
What is Rusts most primative alphabetic type?
char and unlike string literals they are specified with single quotes ‘ ‘
How many bytes in size is Rust char type?
Rust char type is 4 bytes in size and represents a Unicode scalar value, which means it can represent alot more than just ASCII
What is a compound type and what are Rusts two primitive compound types?
Compound types can group multiple values into one type. Rust’s two primitive compound types anre tuples and arrays
What is a tuple in Rust?
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 What is a tuple without any value called?
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
Can the elements of an array be of different types?
No. Unlike a tuple, every element of an array must have the same type.
Are Arrays in Rust fixed in length?
Yes
How would you write an array in Rust?
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]
Why are arrays useful?
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.