The Book, Chapter 10: Beginner Library (and Trait) Question


#1

After a lot of scattered reading here-and-there, I am now going through my shiny new “The Rust Programming Language” book carefully, making sure I can make the examples all work, after typing them with my own fingers (instead of “Yeah, that looks sensible.” and moving on). Helps to play with them, see if I can get my variations to work, too. (Having running code to play with is so valuable.)

I was doing great until I hit the example code that sits between examples 10-13 and 10-14. After carefully telling me to put the trait and impl code in src/lib.rs, the unnumbered example doesn’t say where I am expected to put it, nor how I would get the compiler to find it in said location.

Seems this example assumes I understand the material from Chapter 7 better than I do…

Thanks,

-kb, the Kent who tried to ask on Twitter, hoping it was a stupid question, easily answered with the canonical link for this question, but who was assured it is more than 280-characters worth.


#2

Thanks for asking! So, I agree this is a bit confusing, I’ve filed a bug: https://github.com/rust-lang/book/issues/1445

You’re not expected to put this code anywhere, specifically. It’s intended to show off “look, I can call .summarize on tweets!” but it’s not used in the rest of the chapter or anything. It’s the kind of thing that a binary using your library would do.

Does that make any sense?


#3

Yes, it makes lots of sense.

But the same way that there is a difference between able to speak a foreign language and just understand it, I am trying get everything I am reading in the book to come back out my fingers, and to run.

Newbies (at least this dim one) can’t remember whether Ok is spelled that way or not, nor whether it is part of a Result or an Option—not until we type it a few times.

So I had my break, I ate my lunch, I’ll move on. And try to remember to revisit example “10-13 3/4” once I understand lib.rs and code organization better.

Thanks,

-kb


#4

Great! And hopefully we can make this more clear for others :slight_smile: