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.