13 - closures Flashcards
- What are closures?
2. How are they different from functions?
- Rust’s closures are anonymous functions you can save in a variable or pass as arguments to other functions.
- Unlike functions, closures can capture values from the scope in which they’re defined.
What is a workspace in Rust?
A workspace is a set of packages that share the same Cargo.lock and output directory.
Does Cargo assume that crates in a workspace will depend on each other?
No, we need to explicitly tell cargo in the [dependencies] section in the relevant Cargo.toml
How to run/test a specific package within a workspace directory?
cargo run -p package_name
cargo test -p package_name
Why do we have only one Cargo.lock file in a workspace, rather than having a Cargo.lock in each crate’s directory?
This ensures that all crates are using the same version of all dependencies.
Cargo will resolve the same dependencies in multiple Cargo.toml files to one version and this is recorded n Cargo.lock file.
This is will guarantee that all crates in the workspace will always be compatible with each other.
Where do binaries installed using the cargo install command are stored?
They are stored in the installation root’s bin folder.
If you installed Rust using rustup.rs and don’t have any custom configurations, this directory will be $HOME/.cargo/bin.
*Ensure that directory is in your $PATH to be able to run program you’ve installed with cargo install
Assume you have a binary in your $PATH that is named cargo-something. How can you run that executable from Cargo?
Where does this custom command is listed?
You can run it as if it was a Cargo subcommand by running cargo something.
Custom commands like this are listed when you run cargo –list
When will you use the Box smart pointer?
Typically:
- When you have a type whose size can’t be known at compile time and you want to use a value of that type in a context that requires an exact size.
- When you have a large amount of data and you want to transfer ownership but ensure the data won’t be copied when you do so.
- When you want to own a value and you care only that it’s a type that it’s a type that implements a particular trait rather than being of a specific type.
Why can we use the dereference operator on Box?
Because Box implements the Deref trait.
Why do deref() returns a reference instead of the value itself?
This has to do with Rust's ownership system. If deref() method returned the value directly instead of a reference to the value, the value would be moved out of self. In most cases, we don't want to take ownership of the inner value inside the type that implements Deref
What is deref Coercion?
Deref coercion is a convenience that Rust performs on arguments to functions and methods. Deref coercion works only on types that implement the Deref trait. It converts such a type into a reference to another type.
For example, it can convert &String into $str
When does deref coercion happens?
Rust does deref coercion when it finds types and trait implementations in three cases:
- From &T to &U when T: Deref
- From &mut T to &mut U when T: DerefMut
- From &mut T to &U when T: Deref
Deref coercion happens automatically when we pass a reference to a particular type’s value as an argument to a function or method that doesn’t match the parameter type in the function or method definition. A sequence of calls to the deref method converts the type provided into the type the parameter needs.
Can rust perform deref coercion when going from a immutable reference to a mutable reference? Why?
No.
The borrowing rules state that you can only have one mutable reference to a specific piece of data within a specific scope.
Converting an immutable reference to a mutable reference would require that the initial immutable reference is the only immutable reference to that data, but the borrowing rules don’t guarantee that.
What two important traits are implemented by smart pointers?
The Deref and the Drop traits.
In what order variables will be dropped? Which var drops first?
variables will be dropped in the reverse order of their creation.