R7: Structures Flashcards

1
Q

Basic Structure

A

struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}

fn build_user(email: String, username: String) -> User {
User {
active: true,
username: username,
email: email,
sign_in_count: 1,
}
}

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

Using the Field Init Shorthand

A

fn build_user(email: String, username: String) -> User {
User {
active: true,
username,
email,
sign_in_count: 1,
}
}
}

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

The syntax ..

A

The syntax .. specifies that the remaining fields not explicitly set should have the same value as the fields in the given instance.

fn main() {
// –snip–

let user2 = User {
    email: String::from("another@example.com"),
    ..user1
}; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Tuples Structs

A

struct Color(i32, i32, i32);
struct Point(i32, i32, i32);

fn main() {
let black = Color(0, 0, 0);
let origin = Point(0, 0, 0);
}

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

Unit-Like Structs Without Any Fields

A

struct AlwaysEqual;

fn main() {
let subject = AlwaysEqual;
}

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

Ownership of Struct Data

A

In the User struct definition in Listing 5-1, we used the owned String type rather than the &str string slice type. This is a deliberate choice because we want each instance of this 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
7
Q

Adding Useful Functionality with Derived Traits

A

It’d be useful to be able to print an instance of Rectangle while we’re debugging our program and see the values for all its fields. Listing 5-11 tries using the println! macro as we have used in previous chapters.

Rust does include functionality to print out debugging information, but we have to explicitly opt in to make that functionality available for our struct. To do that, we add the outer attribute #[derive(Debug)] just before the struct definition, as shown in Listing 5-12.

#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}

fn main() {
    let rect1 = Rectangle {
        width: 30,
        height: 50,
    };

    println!("rect1 is {:?}", rect1);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Defining Methods

A

[derive(Debug)]

struct Rectangle {
width: u32,
height: u32,
}

impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}

fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};

println!(
    "The area of the rectangle is {} square pixels.",
    rect1.area()
); }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

associated functions

A

All functions defined within an impl block are called associated functions because they’re associated with the type named after the impl. We can define associated functions that don’t have self as their first parameter (and thus are not methods) because they don’t need an instance of the type to work with. We’ve already used one function like this: the String::from function that’s defined on the String type.

Associated functions that aren’t methods are often used for constructors that will return a new instance of the struct. These are often called new, but new isn’t a special name and isn’t built into the language. For example, we could choose to provide an associated function named square that would have one dimension parameter and use that as both width and height, thus making it easier to create a square Rectangle rather than having to specify the same value twice:

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