CH 5. Using Structs to Structure Related Data Flashcards

1
Q

What is a struct in Rust?

A

A struct is a custom data type that allows you to group related data together.

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

How do you define a struct in Rust?

A

By using the struct keyword followed by the name of the struct and a list of its fields.

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

What is the purpose of the impl block in Rust?

A

The impl block is used to define methods on a struct, allowing you to specify behavior associated with the struct.

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

What is the difference between associated functions and methods in Rust?

A

Associated functions are defined within the impl block but don’t have a self parameter. They are often used as constructors. Methods, on the other hand, have a self parameter and can access and modify the instance of the struct.

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

What is the self keyword in Rust?

A

The self keyword represents an instance of a struct within its methods.

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

What is the purpose of the & symbol in method parameters?

A

The & symbol denotes a reference to the struct instead of taking ownership of it. This allows borrowing the data without moving or copying it.

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

How can you define a tuple struct in Rust?

A

A tuple struct is defined by using the struct keyword followed by the name and specifying the types of the fields without naming them.

struct Triangle(u32, u32, u32)

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

What is the #[derive] attribute used for?

A

The #[derive] attribute allows you to automatically implement common traits, such as Debug or Clone, for a struct.

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