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

Confusing Listings on section 5.1 due to the scope of the struct #2830

Closed
1 of 2 tasks
videbar opened this issue Aug 15, 2021 · 2 comments · Fixed by rust-lang/rust#92231
Closed
1 of 2 tasks

Confusing Listings on section 5.1 due to the scope of the struct #2830

videbar opened this issue Aug 15, 2021 · 2 comments · Fixed by rust-lang/rust#92231
Milestone

Comments

@videbar
Copy link

videbar commented Aug 15, 2021

  • I have checked the latest main branch to see if this has already been fixed
  • I have searched existing issues and pull requests for duplicates

URL to the section(s) of the book with this problem:

https://doc.rust-lang.org/book/ch05-01-defining-structs.html

Description of the problem:

I found the Listings on this section a bit confusing to follow because it is unclear on which scope the struct is being defined.

Listing 5-1 is an example of a struct definition:

struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}

Which then is used on Listing 5-2 to create an instance of the struct User:

    let user1 = User {
        email: String::from("[email protected]"),
        username: String::from("someusername123"),
        active: true,
        sign_in_count: 1,
    };

When I followed this example I put both the struct definition and the creation of user1 in the main function body. Since then, I noticed that the code from Listing 5-1 is not indented suggesting that it is intended to be placed outside the main function, but I think this should may be included in the text.

That on itself is not an issue, since main() is the only function used in these examples. In fact Listing 5-3 is:

    let mut user1 = User {
        email: String::from("[email protected]"),
        username: String::from("someusername123"),
        active: true,
        sign_in_count: 1,
    };

    user1.email = String::from("[email protected]");

Which, as far as I understand, is intended to be included in the main function body, since there's no mention of any other function.

My problem is with Listing 5-4:

fn build_user(email: String, username: String) -> User {
    User {
        email: email,
        username: username,
        active: true,
        sign_in_count: 1,
    }
}

This code follows immediately after Listing 5-3, but it won't work if the struct definition is defined in the main function scope (as Listing 5-3 suggest we do). After following this examples I had the following code:

fn main() {

    struct User {
        username: String,
        email: String,
        sign_in_count: u64,
        active: bool,
    }

}

fn build_user(email: String, username: String) -> User {
    User {
        email: email,
        username: username,
        active: true,
        sign_in_count: 1,
    }
}

Which didn't work because User is not defined in the scope of build_user. I think this can be confusing for new users since the compiler shows the error: cannot find type `Users in this scope and scopes haven't been explained yet at this point in the book.

Suggested fix:

A small sentence explaining in which scope the struct definition should go or including the entire code (with the main() {} definition) should make things clearer.

@arcAman07
Copy link

Hey guys can I take up this issue

@carols10cents
Copy link
Member

No thank you, I'd like to think about it and take care of it. Thanks though!

@carols10cents carols10cents added this to the ch5 milestone Aug 25, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 23, 2021
Update books

## nomicon

1 commits in 49681ea4a9fa81173dbe9ffed74b4d4a35eae9e3..c05c452b36358821bf4122f9c418674edd1d713d
2021-11-24 16:27:28 +0900 to 2021-12-13 15:23:48 +0900
- Update the guidance on uninitialized data with ptr::addr_of_mut (rust-lang/nomicon#325)

## reference

3 commits in 954f3d441ad880737a13e241108f791a4d2a38cd..06f9e61931bcf58b91dfe6c924057e42ce273ee1
2021-11-29 11:11:30 -0800 to 2021-12-17 07:31:40 -0800
- keep consistent for primitive types (rust-lang/reference#1118)
- README.md: link to mdbook docs (rust-lang/reference#1117)
- Say that `...` range patterns are rejected in the 2021 edition (rust-lang/reference#1114)

## book

4 commits in 5f9358faeb1f46e19b8a23a21e79fd7fe150491e..8a0bb3c96e71927b80fa2286d7a5a5f2547c6aa4
2021-12-05 21:33:16 -0500 to 2021-12-22 20:54:27 -0500
- Propagate edits back
- Fix number disagreement. Fixes rust-lang/book#2858.
- Wrap some code in main to make scopes clearer. Fixes rust-lang/book#2830.
- Respond to ch5 nostarch edits

## rustc-dev-guide

9 commits in a374e7d..9bf0028
2021-12-03 09:26:47 -0800 to 2021-12-20 21:53:57 +0900
- remove rustfix item in test intro (rust-lang/rustc-dev-guide#1277)
- Move date-check comment to fix Markdown syntax
- Update humor docs for special-casing ferris emoji
- Fix some broken links (rust-lang/rustc-dev-guide#1274)
- Update rustdoc internals
- Update HIR chapter to use `HirId` instead of `NodeId`
- Fix some broken links
- Update src/getting-started.md
- Improve documentation on r?
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

Successfully merging a pull request may close this issue.

3 participants