Common Programming Concepts. Flashcards
By default Rust variables are ___ ?
immutable
How do you make a variable mutable in Rust?
Adding “mut” in front of the variable name.
What are some differences between variables and constants?
- aren’t allowed to use “mut” with constants.
- Constants are declared with the “const” keyword, not “let”.
- The value of the constant type MUST be annotated.
- Constants can be declared in any scope.
- Constants may only be set to a constant expression.
What is shadowing?
Declaring a new variable with the same name as a previous variable. The new variable shadows the previous variable. The first variable is shadowed by the second.
If one variable shadows another, what value appears when the value is used?
The second one.
How is shadowing different from marking a variable with mut?
A compile-time error will occur if we accidentally try to reassign to this variable without using the let keyword.
What does using let in shadowing allow for?
Perform transformations on a value but have the variable be immutable after those transformations have been completed.
What are two data type subsets found in Rust?
scalar and compound.
Is Rust statically typed?
yes.
What does a scalar data type represent?
A single value.
What are the four primary scalar types found in Rust?
Integers, floating-point numbers, Booleans, and characters.
What is an integer type?
A number without a fractional component.
What does the i and u stand for with integer types?
Signed integer types start with i and unsigned start with u.
List the various lengths for signed/unsigned integer types in Rust.
8-bit 16-bit 32-bit 64-bit 128-bit arch
What does signed and unsigned indicate for integer types?
Whether it’s possible for the number to be negative or positive. Signed can be negative and positive, unsigned is only for positive.
How are signed integer stored?
Two’s compliment representation.
What range of numbers can signed ints store?
-2^(n - 1) to 2^(n - 1) - 1 inclusive.
What range of numbers can unsigned ints store?
0 to 2^(n - 1)
What is Rust’s integer type default?
i32.
What is a floating point type?
Numbers with decimal points.
What are the two floating point types in Rust?
f32 and f64.
What is the default floating point type in Rust
f64 - because on modern CPUs it’s roughly the same speed as f32 but is capable of more precision.
How do you specify a boolean type?
bool.
How many bytes is a boolean type?
one.
How big is the char data type?
Four bytes in size and represents a Unicode Scalar Value.
What is a compound type?
A type that can group multiple values into one type.
What are two primitive compound types in Rust?
Tuples and arrays.
What is a tuple?
A general way of grouping together some number of other values with a variety of types into one compound type.
Do tuples have a fixed length?
Yes, once they are declared they cannot grow or shrink.
How do you create a tuple?
Write a comma-separated list of values inside parentheses.
How can we access tuple elements?
By destructuring (breaking down tuple into individual variables) or a . followed by an index.
What is an array?
A compound data type that groups a collection of multiple values of the same type. Arrays have a fixed length.
What are two reasons why arrays are useful?
When you want your data allocated on the stack rather than the heap or when you want to ensure you always have a fixed number of elements.
What does [i32; 5] in “let a: [i32; 5] = [1, 2, 3, 4, 5];” do.
Specifies an array of 5 i32 integers.
What does “let a = [3; 5];” do?
Initializes an array a of 5 elements all of the value 3.
What is the keyword that allows you to to declare new functions?
fn
In function signatures what must you declare?
The type of each parameter.
What is a statement in Rust?
An instruction that performs some action and does not return a value.
What is an expression in Rust?
An instruction that performs some action and does return some value.
What does adding a ; to an expression do?
Turns it into a statement. It will not return anything.
Where does a return type get declared in a function signature?
after the ->
What must comments start with in Rust?
//
Is if an expression or a statement in Rust?
An expression.
Since if is an expression, what does it allow for?
Using it on the right side of a let statement.
What are the three types of loops Rust has?
loop, while, for.
What does the loop keyword do?
Tells Rust to execute a block of code over and over again forever or until you explicitly tell it to stop.
What is a way to pass the result of a loop operation to the rest of your code?
Add the value you want returned after the break expression in the loop.
What does a while loop do in Rust?
While a condition holds true, the code runs; otherwise, it exits the loop.
What does a for loop do in Rust?
Executes some code for each item in a collection