Structs Flashcards

1
Q

What is a struct?

A

A type this is composed of other types.

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

How are structs different then tuples?

A

Each value in a struct is named.

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

What keyword is used to create a struct?

A

struct.

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

What is the syntax for creating a struct?

A

struct struct_name {
field_name: type,
}

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

How do we create an instance of a struct?

A

State the name of the struct and then add curly brackets containing key: value pairs, where the keys are the names of the fields and the values are the data we want to store in those fields.

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

How do we access a specific value from a struct?

A

dot notation. e.g. user.email

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

What is the field init short hand

A

Pattern that allows for only writing the variable name when assigning it to a struct field if they share the same name. email: email can be written as just email.

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

What is the struct update syntax?

A
Any fields not explicitly set can be set from another struct instance with ..  e.g. 
let user2 = User {
    email
    foo: String::from("bar"),
    ..user1
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a tuple struct

A

A tuple that is named. Individual fields are not named.

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

When are tuple structs useful?

A

When you want to give the whole tuple a name and make the tuple be a different type from other tuples, and naming each field as in a regular struct would be verbose or redundant.

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

What are unit-like structs

A

structs without any fields.

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

When are unit-like structs useful?

A

Situations in which you need to implement a trait on some type but don’t have any data that you want to store in the type itself.

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

How are methods different than functions?

A

They are declared within the context of a struct, enum, or trait object and their first parameter is always self.

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

How do we define a function within the context of a struct?

A
We start an impl (implementation) block. e.g. 
impl Rectangle {
    fn area(&self) -> u32 {
        self.width * self.height
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the first parameter to a method?

A

self.

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

Describe how automatic referencing and dereferencing works?

A

When you call a method with object.something(), Rust automatically adds in &, &mut, or * so object matches the signature of the method.

17
Q

What is an associated function?

A

A function that does not take self as the first parameter. They often associated with a struct and used as a Constructor.

18
Q

What is the syntax of an associated function

A

:: with struct name. e.g. let sq = Rectangle::square(3);

19
Q

How many impl blocks is a struct allowed to have?

A

Multiple.