CH 3. Common Programming Concepts Flashcards
In Rust what does the :: syntax mean?
It indicates the use of an associated function of the given type. For example String::new(), new() is the function that is associated with the type String.
In Rust what does the & syntax mean?
It indicates that this argument is a reference, which gives you a way to let multiple parts of your code access to one piece of data without needing to copy that data into memory multiple times.
What is an enum or enumeration?
It is a type that can be in one of multiple possible states. For example in enum Result<T, E>, contains the states, Ok(T) which represents a success and contains a value of Type T, Err(E) is the other state which represents an error and contains the error value.
What is the cargo.lock file and why is it needed?
Cargo has a mechanism that ensures you can rebuild the same artifact every time you or anyone else builds your code: Cargo will use only the versions of the dependencies you specified until you indicate otherwise. For example, say that next week version 0.8.4 of the rand crate comes out, and that version contains an important bug fix, but it also contains a regression that will break your code. To handle this, Rust creates the Cargo.lock file the first time you run cargo build.
What does cargo update do?
It updates the crate which will figure out all of the latest versions that fit your specifications in your cargo.toml. Then cargo will write those versions to the cargo.lock file.
What does the command cargo doc –open do?
It will build documentation provided by all of your dependencies locally and open it in your browser.
What is a match expression?
Its an expression with multiple arms. Each arm consists of a pattern to match against and the code that should be run if the value given to match fits the arms pattern.
What is shadowing?
Shadowing is when you reuse the same variable name rather than creating two unique variables.
What does the trim() method do?
The trim() method will will remove whitespace at the beginning and end of a String as well as remove newline(\n) and carriage return(\r).
What does the parse() method do?
Used on string will convert a string to another type. The parse() method could fail since it cant convert some characters to a number (ie. $,@!%) so the method returns a Result type.
What is the differences between constants and variables in rust?
Constants are always immutable. They are declared using the const keyword instead of let. The type of the value MUST be annotated. They can be declared in any scope. Last difference is that constants may be set only to a constant expression, not the result of a value that could only be computed at runtime.
Rust is a statically typed language, what does that mean?
It means that Rust must know the types of all variables at compile time.
What is a scalar type and what are the four primary scalar types of rust?
A scalar type represents a single value, such as integers floating-point numbers, booleans, and characters.