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
Q

Flashcard Question

A

Flashcard Answer

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

Flashcard Question

A

Flashcard Answer

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

Flashcard Question

A

Flashcard Answer

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

Flashcard Question

A

Flashcard Answer

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

Flashcard Question

A

Flashcard Answer

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

Flashcard Question

A

Flashcard Answer

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

Flashcard Question

A

Flashcard Answer

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

Flashcard Question

A

Flashcard Answer

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

Flashcard Question

A

Flashcard Answer

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

Flashcard Question

A

Flashcard Answer

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

Flashcard Question

A

Flashcard Answer

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

Flashcard Question

A

Flashcard Answer

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

Flashcard Question

A

Flashcard Answer

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

Flashcard Question

A

Flashcard Answer

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

Flashcard Question

A

Flashcard Answer

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

Flashcard Question

A

Flashcard Answer

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

Flashcard Question

A

Flashcard Answer

42
Q

Flashcard Question

A

Flashcard Answer

43
Q

Flashcard Question

A

Flashcard Answer

44
Q

Flashcard Question

A

Flashcard Answer

45
Q

Flashcard Question

A

Flashcard Answer

46
Q

Flashcard Question

A

Flashcard Answer

47
Q

Flashcard Question

A

Flashcard Answer

48
Q

Flashcard Question

A

Flashcard Answer

49
Q

Flashcard Question

A

Flashcard Answer

50
Q

Flashcard Question

A

Flashcard Answer

51
Q

Flashcard Question

A

Flashcard Answer

52
Q

Flashcard Question

A

Flashcard Answer

53
Q

Flashcard Question

A

Flashcard Answer

54
Q

Flashcard Question

A

Flashcard Answer

55
Q

Flashcard Question

A

Flashcard Answer

56
Q

Flashcard Question

A

Flashcard Answer

57
Q

Flashcard Question

A

Flashcard Answer

58
Q

Flashcard Question

A

Flashcard Answer

59
Q

Flashcard Question

A

Flashcard Answer

60
Q

Flashcard Question

A

Flashcard Answer

61
Q

Flashcard Question

A

Flashcard Answer

62
Q

Flashcard Question

A

Flashcard Answer

63
Q

Flashcard Question

A

Flashcard Answer

64
Q

Flashcard Question

A

Flashcard Answer

65
Q

Flashcard Question

A

Flashcard Answer

66
Q

Flashcard Question

A

Flashcard Answer

67
Q

Flashcard Question

A

Flashcard Answer

68
Q

Flashcard Question

A

Flashcard Answer

69
Q

Flashcard Question

A

Flashcard Answer

70
Q

Flashcard Question

A

Flashcard Answer

71
Q

Flashcard Question

A

Flashcard Answer

72
Q

Flashcard Question

A

Flashcard Answer

73
Q

Flashcard Question

A

Flashcard Answer

74
Q

Flashcard Question

A

Flashcard Answer

75
Q

Flashcard Question

A

Flashcard Answer

76
Q

Flashcard Question

A

Flashcard Answer

77
Q

Flashcard Question

A

Flashcard Answer

78
Q

Flashcard Question

A

Flashcard Answer

79
Q

Flashcard Question

A

Flashcard Answer

80
Q

Flashcard Question

A

Flashcard Answer

81
Q

Flashcard Question

A

Flashcard Answer

82
Q

Flashcard Question

A

Flashcard Answer

83
Q

Flashcard Question

A

Flashcard Answer

84
Q

Flashcard Question

A

Flashcard Answer

85
Q

Flashcard Question

A

Flashcard Answer

86
Q

Flashcard Question

A

Flashcard Answer

87
Q

Flashcard Question

A

Flashcard Answer

88
Q

Flashcard Question

A

Flashcard Answer

89
Q

Flashcard Question

A

Flashcard Answer

90
Q

Flashcard Question

A

Flashcard Answer

91
Q

Flashcard Question

A

Flashcard Answer

92
Q

Flashcard Question

A

Flashcard Answer

93
Q

Flashcard Question

A

Flashcard Answer

94
Q

Flashcard Question

A

Flashcard Answer

95
Q

Flashcard Question

A

Flashcard Answer

96
Q

Flashcard Question

A

Flashcard Answer

97
Q

Flashcard Question

A

Flashcard Answer

98
Q

Flashcard Question

A

Flashcard Answer

99
Q

Flashcard Question

A

Flashcard Answer

100
Q

Flashcard Question

A

Flashcard Answer