Terms Flashcards
Statically typed
Data type of each variable must be known at compile time.
What are Rusts four primary scalar types?
Integers, Floating points, Characters, Booleans.
What is an integer data type?
A number without a fractional part.
What is a panic() in rust?
When a program exits with an error.
How does Rust handle integer overflow?
At runtime when debugging: it panics() At runtime on release, it wraps (i.e., from 255 -> 1).
What memory does a Boolean value take up?
One byte, nom.
How are character literals differentiated from string literals in Rust?
Strings use double quotes, characters use single quotes.
What is an approximate CS definition of a tuple?
An immutable ordered sequence of elements.
How do arrays differ from tuples in Rust?
They are declared with square brackets instead of round, all elements are of the same data type.
What is the difference between a statement and an expression?
A statement is an instruction to perform some action, whereas an expression evaulates code to a resulting value.
What happens at the e.o.l with a statement or expression in Rust?
Statements end in a semi colon, expressions do not.
What is the difference between shadowing and mutability?
When a mutable variable is modified, the value in memory is modified. When an immutable variable is shadowed, a new memory location is used to store a new value - with the variable name. The old memory location is unused.
What kind of loops does Rust have?
while, for, loop
What is the stack?
A memory structure that is strictly first in last out. Push onto stack, or pop off the stack.
What is the heap?
A memory structure that allows “random” storage and retrieval of allocations.
What is allocating on the heap?
Find an empty (and large enough) space on the heap, mark it as being used, and return a pointer to that location.
Is pushing onto the stack or allocating on the heap faster?
Pushing on the stack!
What calling a function with arguments, what memory structure is used?
The stack. Any arguments and local variables are pushed onto the stack.
What is boilerplate code?
Refers to code that must be include with little to no alteration.
What is verbose code?
Containing more explanation than necessary, or more code than necessary.
What is meant by a string “literal”?
Text that is hard coded in the program.
What is RAII?
Resource allocation is initialization. The curly bracket at the end of a code block automatically drops any variables that go out of scope from memory.
Will this code run? Why?
fn main() {
let hello = String::from(“hi”);
say_hi(hello);
println!(“saying {} again”, hello);
}
fn say_hi(hello: String) {
println!(“{}”, hello);
}
Nope, because hello is moved to the scope of say_hi and not returned. Once a variable-value binding goes out of scope it is dropped from memory.
What is a String slice?
A reference to a part of a string! Initialized and declared with:
&s[0..5]
Where the brackets contain the index range (upper bound exclusive).
How can the length of a string be found?
Using:
variable_name.len()