Rust Flashcards
What is Rust primarily used for?
Rust is used for systems programming, particularly where safety and concurrency are critical, such as in operating systems, game engines, and web browsers.
How do you define a variable in Rust?
Use the let
keyword to define a variable. For example, let x = 5;
defines an immutable variable x
with the value 5
.
What keyword is used to make a variable mutable in Rust?
Use the mut
keyword to make a variable mutable. For example, let mut x = 5;
allows x
to be modified later.
Explain ownership in Rust.
Ownership is a set of rules enforced at compile time that governs how memory and other resources are managed in Rust. It ensures memory safety and concurrency without needing a garbage collector.
What is a borrow in Rust?
A borrow in Rust allows access to data without taking ownership of it, using either a reference (&
) for shared access or a mutable reference (&mut
) for exclusive access.
Provide a basic example of a function in Rust.
A simple function in Rust:
rust fn add_two(x: i32) -> i32 { x + 2 }This function
add_two
takes an integer x
and returns x
plus 2.How do you create a new vector in Rust?
Use Vec::new()
or the vec!
macro for initialization. For example, let v = vec![1, 2, 3];
initializes a vector with the values 1, 2, and 3.
Describe pattern matching in Rust.
Pattern matching in Rust is used to branch execution paths based on the structure and values of data. It is typically implemented using the match
statement.
How do you handle errors in Rust?
Rust uses Result
for recoverable errors and panic!
for unrecoverable errors, allowing explicit handling of error conditions.
What is a trait in Rust?
A trait in Rust defines functionality a type must provide. It is similar to an interface in other languages, enabling polymorphism.
How do you declare a constant in Rust?
Constants are declared using the const
keyword. For example, const MAX_POINTS: u32 = 100_000;
.
What is a slice in Rust and how is it used?
A slice is a reference to a contiguous sequence of elements in a collection rather than the whole collection. They are used to allow safe, efficient access to a portion of an array without copying.
Describe how to use a struct in Rust.
Structs in Rust are used to create custom data types. Example:
rust struct User { name: String, age: u32, }
What does the impl
keyword do in Rust?
The impl
keyword is used to define implementations on types, including methods associated with structs and enums.
Give an example of enum usage in Rust.
Enums in Rust are used to define a type which can be one of several variants. Example:
rust enum Message { Quit, Move { x: i32, y: i32 }, Write(String), }
What is cargo and how is it used in Rust?
Cargo is the Rust package manager and build system, used to manage Rust projects, dependencies, and to build and test projects.
Explain how modules work in Rust.
Modules in Rust are used to organize code into namespaces and control the visibility (public/private) of items with mod
and pub
.
What are Rust lifetimes?
Lifetimes are annotations in Rust that describe the scopes for which references are valid, ensuring safe memory access by preventing dangling references.
How to perform error handling with Option
in Rust?
Option
is used in Rust for values that may be None
(absence of value) or Some(value)
. It’s used to handle the possibility of missing values safely.
Provide an example of using match
with enums in Rust.
Example of using match
with enums:
rust enum State { Working, Stopped, } let state = State::Working; match state { State::Working => println!("Working"), State::Stopped => println!("Stopped"), }
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer
Flashcard Question
Flashcard Answer