The original French quote appears to be from here. Stories link to another tweet (since privated) as the source of the translation, which quotes the first tweet, but it only differs from the embedded Google Translate result by a single word (“tasty” vs. “satisfying”). Here’s a video of the press conference with more context and a similar translation of that quote.
I’ll preface this by saying I’m working my way through the Rust book, too–just a bit further along–so don’t take my word as gospel.
This exact scenario is what the
?
operator was designed for: returning early with theErr
if one is received[1], otherwise unpacking theOk
. As you’ve discovered, it’s a common pattern, so using the?
operator greatly cuts down on the boilerplate code. If you wanted to do the equivalent of you have here (panicking instead of returning theErr
for it to potentially be handled in calling code, albeit without your custom panic messages[2]) you could achieve this withunwrap()
instead of?
:let html_content_text = reqwest::blocking::get(&permalink).unwrap().text().unwrap();
Both of these will be covered in chapter 9.
If you want to avoid those constructs until later, the only thing I’d say is that some of the intermediate variables seem unnecessary since you can match on the function call directly:
fn get_document(permalink: String) -> Html { let html_content = match reqwest::blocking::get(&permalink) { Ok(response) => response, Err(error) => panic!("There was an error making the request: {:?}", error), }; let html_content_text = match html_content.text() { Ok(text) => text, Err(error) => panic!( "There was an error getting the html text from the content of response: :{:?}", error ), }; let document = Html::parse_document(&html_content_text); document }
You could also eliminate the final
let
statement and just stick theparse_document
call at the end, but that’s a matter of preference–I know having an intermediate variable before a return can sometimes make debugging easier.As for whether you should build something now or wait till you learn more–go with your gut! The most important thing is that you stay actively engaged with the material, and many people find diving into projects as soon as possible helps them learn and stay motivated. You could also use rustlings and/or Rust by Example as you go through the book, which is what I’ve been doing (specifically rustlings). It’s not as stimulating as writing a project from scratch, but it does let you write some relevant code. And if you’re not already, I highly recommend using the Brown version of the Rust Book which includes interactive quizzes sprinkled throughout. I’ve found them particularly helpful for understanding the quirks of the borrow checker, which is a topic it continues to revist throughout the book.
There’s also some type coercion, but that’s beyond the scope of your question ↩︎
edit: you can use
expect
to get the custom messages as covered in another comment–not sure how I forgot that ↩︎