Hello,
I'm studing Rust: what an interesting new language for me!
I found an errata in the Rust's book section 6.1
enum Message has WriteMessage as a member type.
However, the next code example says:
let m = Message::Write(String::from("hello"));
That's wrong, I think it must say:
let m = Message::WriteMessage(String::from("hello"));
That's all.
erelde
2
jofas
3
Its in this section at the end: Defining an Enum - The Rust Programming Language
But I don't think this is an error. First snippet defines Message
as:
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
which is correctly instantiated by:
let m = Message::Write(String::from("hello"));
from this snippet:
impl Message {
fn call(&self) {
// method body would be defined here
}
}
let m = Message::Write(String::from("hello"));
m.call();
In between these snippets is the definition of a tuple struct:
struct WriteMessage(String); // tuple struct
which could be instantiated with:
let m = WriteMessage(String::from("hello"));
WriteMessage
is a struct, not a member of the Message
enum.
Ok,
I see.
Thank you.
Newbie rust student error.
jofas
5
No worries. This snippet is wrongly formatted (indentation-wise):
impl Message {
fn call(&self) {
// method body would be defined here
}
}
let m = Message::Write(String::from("hello"));
m.call();
You could open a PR fixing the indentation 
... there are a lot of snippets on this page with leading indentation I just saw
erelde
6
Yes. I saw that, but I left open the possibility that I didn't see the error @jopeless saw.
1 Like
There was no error, I thought WriteMessage was a member of the enum and it is a struct written as another example. So, it was my fault.
system
Closed
8
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.