Skip to content

Chapter 5.2 - Incomplete error code blocks. #4640

@virtuallyunknown

Description

@virtuallyunknown

URL to the section(s) of the book with this problem:

https://doc.rust-lang.org/book/ch05-02-example-structs.html#adding-functionality-with-derived-traits

Description of the problem:

In chapter 5.2 - Adding Functionality with Derived Traits, there is a demonstration how you need to make some adjustments to be able to use the println! macro with structs.

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

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

    println!("rect1 is {rect1}");
}

It reads:

If we continue reading the errors, we’ll find this helpful note:

| Rectangle cannot be formatted with the default formatter
| required by this formatting parameter

Then it continues:

Let’s try it! The println! macro call will now look like println!("rect1 is {rect1:?}");...

Let's try what what exactly? If I run the provided code example locally, you get this exact error message (which is also a bit different from the book, maybe it was written with a different version).

Rectangle doesn't implement std::fmt::Display
in format strings you may be able to use {:?} (or {:#?} for pretty-print) instead

Suggested fix:

Now it becomes pretty obvious what the author meant by "Let’s try it", and the following example makes sense. Also, below this error block there is another one which seems to be incomplete:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions