Structs Flashcards

1
Q

What is a struct?

A

A custom data type that lets you name and package together multiple related values that make up a meaningful group.

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

What are the names and types of the pieces of data inside a struct?

A

fields

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

How do you create an instance of a struct?

A

by specifying concrete values for each of the fields.

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

How do you get a specific value from a struct?

A

Using dot notation

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

Can certain fields in a struct be marked mutable?

A

No, the entire instance must be mutable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
What type of syntax is used to specify a property and its value that have the same name, as in:
struct {
   name,
   email
}
A

field init shorthand syntax

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

How do you create a new instance of a struct that uses most of the values of a previous instance?

A
Using struct update syntax:
let user2 = User {
  email: "someone@someplace.com",
  username: "johnsonw", 
  ...user1,
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are tuple structs?

A

They have the added meaning the struct name provides but don’t have names associated with their fields. These are useful when you want to give the tuple a name such that it is different from other tuples.

struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you access an element on a tuple or tuple struct?

A

By using a “.” followed by an index to access an individual value.

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

What is a unit struct?

A

A struct that doesn’t have any fields

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

When is a unit struct useful?

A

when 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
12
Q

Should structs own most of their data?

A

Generally, we want instances of a struct to own all of its data and for that data to be valid for as long as the entire struct is valid.

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

Can data in a struct store a reference to data owned by something else?

A

Yes, but to do so requires the use of lifetimes.

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

What does a lifetime guarantee for a struct?

A

Lifetimes ensure that the data referenced by a struct is valid for as long as the struct is.

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

Should you prefer taking ownership of a struct or borrowing it?

A

You should prefer borrowing a struct.

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

What trait does the “{}” tell println! to format with?

17
Q

What is the Display trait intended for?

A

Direct end user consumption

18
Q

Why can’t structs be printed with the Display trait?

A

The way they should be printed is less clear because there are more display possibilities.

19
Q

Which trait do we use to print a struct?

20
Q

How do we use the debug trait when printing a struct?

21
Q

How do you include the Debug trait on a struct so that it can be printed?

A

Add the #[derive(Debug)] annotation just before the struct definition

22
Q

When using a large struct, it’s easier to read the output when it is formatted. How do we print a large struct in a way that is easier to read than when using {:?}?

23
Q

How are methods different from functions?

A

They are defined within the context of a struct, enum, or trait object and their first parameter is always “self”, which represents the instance of the struct the method is being called on.

24
Q

Where do you define a method?

A

Inside of an impl (implementation) block for the struct

25
What are the three ways in which a method can take "self"?
1. It can take ownership 2. It can borrow self immutably 3. It can borrow self mutably
26
If you just want to read data from the struct how should a method take "self"?
As an immutable reference
27
If you want to read and write data on a struct how should a method take "self"?
As a mutable reference
28
When would a method take ownership of "self"?
It's rare. It usually is done when the method transforms self into something else and you want to prevent the caller from using the original instance after the transformation.
29
What is the main benefit of using methods instead of functions?
organization
30
Why doesn't rust have the "->" operator like c++?
Rust uses a feature called automatic referencing and dereferencing.
31
How does autmomatic referencing / defreferencing work?
When you call a method with object.something(), Rust automatically adds in &, &mut, or * so object matches the signature of the method.
32
Why is automatic referencing / dereferencing allowed to work?
because methods have a clear receiver -> self
33
What are associated functions?
Functions within impl blocks that don't take self as a parameter.
34
What are associated functions often used for?
creating constructors that will return a new instance of the struct
35
How do you access an associated function?
StructName::associated_function_name()
36
Can structs have multiple impl blocks?
Yes
37
What do structs let you create?
custom types that are meaningful for your domain
38
What do associated functions let you do?
namespace functionality that is particular to your struct without having an instance available