Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function1 and function2 should compile from visible 7-15. #1882

Closed
jpgoldberg opened this issue Mar 24, 2019 · 1 comment
Closed

function1 and function2 should compile from visible 7-15. #1882

jpgoldberg opened this issue Mar 24, 2019 · 1 comment

Comments

@jpgoldberg
Copy link

Listing 7-15 (7-19 on current version of doc.rust-lang.org/book) is

use std::fmt;
use std::io;

fn function1() -> fmt::Result {
#     Ok(())
}

fn function2() -> io::Result<()> {
#     Ok(())
}

without the return values for the functions being visible to someone reading the rendered book, the text that they experiment with will not compile.

This is confusing for the reader for two reasons

  1. (Almost?) every visible listing prior to this one does build as expected.
  2. The context of this listing specifically asks people to construct an erroneous one to see that code as listed is correct, while the erroneous variant produces an error.

Specifically with regard to point (2), the text that follows the listing contains

If instead we specified use std::fmt::Result and use std::io::Result, we’d
have two Result types in the same scope and Rust wouldn’t know which one we
meant when we used Result. Try it and see what compiler error you get!

The clear implication is that the reader should not expect a compiler error in the listing. So even if there are cases where we want to allow fragmentary listings, listings 7-15 and 7-16 are not such listings.

Suggesting fixes

  1. Simply expose the Ok(()) expressions in the listings 7-15 and 7-16.
  2. Find another example in which either there is no return value or the return value is something simpler for the new user.

I do understand that (1) involves taking up extra visible space. As for (2), I do not know Rust or the standard crates well enough to offer anything specific. (I am only on Chapter 7 of the book.)

This issue is prompted by discussion on the forums: https://users.rust-lang.org/t/solved-book-ch07-02-problem-building-example-with-io-result/26595

@carols10cents
Copy link
Member

Thanks! I've opted to add --snip-- comments that we use to indicate that some code has been elided, and removed the encouragement to try compiling this code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants