Rust Flashcards

1
Q

What is Rust primarily used for?

A

Rust is used for systems programming, particularly where safety and concurrency are critical, such as in operating systems, game engines, and web browsers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you define a variable in Rust?

A

Use the let keyword to define a variable. For example, let x = 5; defines an immutable variable x with the value 5.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What keyword is used to make a variable mutable in Rust?

A

Use the mut keyword to make a variable mutable. For example, let mut x = 5; allows x to be modified later.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain ownership in Rust.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a borrow in Rust?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Provide a basic example of a function in Rust.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you create a new vector in Rust?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Describe pattern matching in Rust.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you handle errors in Rust?

A

Rust uses Result for recoverable errors and panic! for unrecoverable errors, allowing explicit handling of error conditions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a trait in Rust?

A

A trait in Rust defines functionality a type must provide. It is similar to an interface in other languages, enabling polymorphism.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you declare a constant in Rust?

A

Constants are declared using the const keyword. For example, const MAX_POINTS: u32 = 100_000;.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a slice in Rust and how is it used?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe how to use a struct in Rust.

A

Structs in Rust are used to create custom data types. Example:

rust struct User { name: String, age: u32, }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the impl keyword do in Rust?

A

The impl keyword is used to define implementations on types, including methods associated with structs and enums.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Give an example of enum usage in Rust.

A

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), }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is cargo and how is it used in Rust?

A

Cargo is the Rust package manager and build system, used to manage Rust projects, dependencies, and to build and test projects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Explain how modules work in Rust.

A

Modules in Rust are used to organize code into namespaces and control the visibility (public/private) of items with mod and pub.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are Rust lifetimes?

A

Lifetimes are annotations in Rust that describe the scopes for which references are valid, ensuring safe memory access by preventing dangling references.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How to perform error handling with Option in Rust?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Provide an example of using match with enums in Rust.

A

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"), }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Flashcard Question

A

Flashcard Answer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Flashcard Question

A

Flashcard Answer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Flashcard Question

A

Flashcard Answer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Flashcard Question

A

Flashcard Answer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Flashcard Question
Flashcard Answer
26
Flashcard Question
Flashcard Answer
27
Flashcard Question
Flashcard Answer
28
Flashcard Question
Flashcard Answer
29
Flashcard Question
Flashcard Answer
30
Flashcard Question
Flashcard Answer
31
Flashcard Question
Flashcard Answer
32
Flashcard Question
Flashcard Answer
33
Flashcard Question
Flashcard Answer
34
Flashcard Question
Flashcard Answer
35
Flashcard Question
Flashcard Answer
36
Flashcard Question
Flashcard Answer
37
Flashcard Question
Flashcard Answer
38
Flashcard Question
Flashcard Answer
39
Flashcard Question
Flashcard Answer
40
Flashcard Question
Flashcard Answer
41
Flashcard Question
Flashcard Answer
42
Flashcard Question
Flashcard Answer
43
Flashcard Question
Flashcard Answer
44
Flashcard Question
Flashcard Answer
45
Flashcard Question
Flashcard Answer
46
Flashcard Question
Flashcard Answer
47
Flashcard Question
Flashcard Answer
48
Flashcard Question
Flashcard Answer
49
Flashcard Question
Flashcard Answer
50
Flashcard Question
Flashcard Answer
51
Flashcard Question
Flashcard Answer
52
Flashcard Question
Flashcard Answer
53
Flashcard Question
Flashcard Answer
54
Flashcard Question
Flashcard Answer
55
Flashcard Question
Flashcard Answer
56
Flashcard Question
Flashcard Answer
57
Flashcard Question
Flashcard Answer
58
Flashcard Question
Flashcard Answer
59
Flashcard Question
Flashcard Answer
60
Flashcard Question
Flashcard Answer
61
Flashcard Question
Flashcard Answer
62
Flashcard Question
Flashcard Answer
63
Flashcard Question
Flashcard Answer
64
Flashcard Question
Flashcard Answer
65
Flashcard Question
Flashcard Answer
66
Flashcard Question
Flashcard Answer
67
Flashcard Question
Flashcard Answer
68
Flashcard Question
Flashcard Answer
69
Flashcard Question
Flashcard Answer
70
Flashcard Question
Flashcard Answer
71
Flashcard Question
Flashcard Answer
72
Flashcard Question
Flashcard Answer
73
Flashcard Question
Flashcard Answer
74
Flashcard Question
Flashcard Answer
75
Flashcard Question
Flashcard Answer
76
Flashcard Question
Flashcard Answer
77
Flashcard Question
Flashcard Answer
78
Flashcard Question
Flashcard Answer
79
Flashcard Question
Flashcard Answer
80
Flashcard Question
Flashcard Answer
81
Flashcard Question
Flashcard Answer
82
Flashcard Question
Flashcard Answer
83
Flashcard Question
Flashcard Answer
84
Flashcard Question
Flashcard Answer
85
Flashcard Question
Flashcard Answer
86
Flashcard Question
Flashcard Answer
87
Flashcard Question
Flashcard Answer
88
Flashcard Question
Flashcard Answer
89
Flashcard Question
Flashcard Answer
90
Flashcard Question
Flashcard Answer
91
Flashcard Question
Flashcard Answer
92
Flashcard Question
Flashcard Answer
93
Flashcard Question
Flashcard Answer
94
Flashcard Question
Flashcard Answer
95
Flashcard Question
Flashcard Answer
96
Flashcard Question
Flashcard Answer
97
Flashcard Question
Flashcard Answer
98
Flashcard Question
Flashcard Answer
99
Flashcard Question
Flashcard Answer
100
Flashcard Question
Flashcard Answer