Rust Interview Questions
Last Updated: 2023-09-19
Why Rust?
- syntactically similar to C++, but can guarantee memory safety.
- achieves memory safety without garbage collection.
Crucial features:
- move semantics
- minimal runtime
- trait-based generics
- zero cost abstractions
- pattern matching
- type inference.
What are Modules in Rust?
- Importing Modules with
use
- Re-exporting with
pub use
What are "Cargo" and "Crate" in Rust?
- Cargo: a build system and package manager.
- Crate: a library or package.
Each crate has an implicit root module that contains the code for that crate. You can then define a tree of sub-modules under that root module. Modules allow you to partition your code within the crate itself.
Methods vs Associated Functions
You can have two kinds of procedures: methods and associated functions.
- methods: exists inside the struct implementation and receives the
&self
parameter, indicating that it needs to be called by an instance with a.
. - associated functions: called without an instance, it can be compared with the Java static functions. They are normally are used as a constructor of instances.
Name some Rust projects?
- https://github.com/denoland/deno: A modern runtime for JavaScript and TypeScript.
- https://github.com/swc-project/swc: A javascript/typescript compiler, used by Next.js.
- https://github.com/bevyengine/bevy: A refreshingly simple data-driven game engine built in Rust.
- https://github.com/uutils/coreutils: coreutils written in Rust.
Trending Rust projects on GitHub: https://github.com/trending/rust
When to use &self
, self
and &mut self
?
&self
: when read-only reference is rquired.self
: when a value is to be consumed by the function.&mut
: when a value needs to be mutated by the function.