1/17
from “A Half-Hour to Learn Rust”
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What makes Rust different from other system languages like C/C++?
Rust focuses on memory safety and concurrency without needing a garbage collector.
Are variables immutable or mutable by default in Rust?
Immutable by default. Use mut
to make them mutable. This design choice minimizes bugs related to unintended variable changes.
What does the let
keyword do in Rust?
It declares a variable, which is immutable unless mut
is added.
What is the purpose of the match
keyword in Rust?
It allows pattern matching, similar to a powerful switch-case statement.
What's the difference between struct
and enum
in Rust?
struct
groups related fields into a typeenum
defines a type with multiple possible variants
How do you define a function in Rust?
Using fn
.
Example: fn add(a: i32, b: i32) -> i32 { a + b }
What does impl
do in Rust?
It defines methods for a struct
or enum
.
What is ownership in Rust?
A memory model where each value has one owner; when ownership is transferred, the original variable is no longer valid.
What is borrowing in Rust?
Letting a function access a variable via a reference (&
) without taking ownership.
What is mutable borrowing?
A reference (&mut
) that allows modifying the borrowed data, but only one mutable borrow is allowed at a time.
How do vectors (Vec
) work in Rust?
They are growable arrays. You can push
, pop
, and iterate over them.
How does error handling work in Rust?
Via Result<T, E>
for recoverable errors and panic!()
for unrecoverable ones.
What is the Option<T>
type in Rust?
A type that either contains Some(value)
or None
, avoiding nulls.
How do you match on a Result
type?
Using match
to handle Ok(value)
and Err(error)
branches.
What is a trait in Rust?
A collection of methods that types can implement (similar to interfaces in other languages).
What are lifetimes in Rust?
Annotations that tell the compiler how long references should be valid.
How do you write a unit test in Rust?
Use #[test]
before the test function and use assert_eq!()
to check values
What is the Rust compiler’s biggest strength for beginners?
It gives detailed, helpful error messages, especially around ownership and types.