-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Description
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:
|
Rectanglecannot 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).
Rectangledoesn't implementstd::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:
