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

ch07-03-paths-for-referring-to-an-item-in-the-module-tree: field is never read: seasonal_fruit note: #[warn(dead_code)] on by default #3087

Closed
HenrikBach1 opened this issue Mar 18, 2022 · 7 comments

Comments

@HenrikBach1
Copy link

URL to the section(s) of the book with this problem:
https://doc.rust-lang.org/book/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html

Description of the problem:
Listing 7-9 cannot compile, even though you create a main function calling the function eat_at_restaurant, due to the compiler complaints about the seasonal_fruit field of Breakfast:

[{
	"resource": "/projects/rust/paths-in-modules-7_3/src/main.rs",
	"owner": "rust-file:///projects/rust/paths-in-modules-7_3",
	"code": "dead_code",
	"severity": 4,
	"message": "field is never read: `seasonal_fruit`\n\nnote: `#[warn(dead_code)]` on by default",
	"source": "rustc",
	"startLineNumber": 4,
	"startColumn": 9,
	"endLineNumber": 4,
	"endColumn": 31
}]
vscode ➜ /projects/rust/paths-in-modules-7_3 (master ✗) $ rustc --version
rustc 1.59.0 (9d1b2106e 2022-02-23)

Complete code:

mod back_of_house {
    pub struct Breakfast {
        pub toast: String,
        seasonal_fruit: String,
    }

    impl Breakfast {
        pub fn summer(toast: &str) -> Breakfast {
            Breakfast {
                toast: String::from(toast),
                seasonal_fruit: String::from("peaches"),
            }
        }
    }
}

pub fn eat_at_restaurant() {
    // Order a breakfast in the summer with Rye toast
    let mut meal = back_of_house::Breakfast::summer("Rye");
    // Change our mind about what bread we'd like
    meal.toast = String::from("Wheat");
    println!("I'd like {} toast please", meal.toast);

    // The next line won't compile if we uncomment it; we're not allowed
    // to see or modify the seasonal fruit that comes with the meal
    // meal.seasonal_fruit = String::from("blueberries");
}

fn main ()
{
    eat_at_restaurant()
}

Suggested fix:

@MarkDBlackwell
Copy link

Listing 7-9 should be prepended with #![allow(dead_code)] similar to #3081.

@carols10cents
Copy link
Member

Hi, please search existing issues before filing new issues. If you had searched dead_code, you would have found this issue and others where I responded:

There are many examples in the book that will get dead code warnings because we are showing small pieces as digestible examples, not full production code. Thanks though!

@MarkDBlackwell
Copy link

@carols10cents Thank you for all your wonderful work!

Would you welcome a single pull request prepending relevant allow statements to each listing? :)

I feel it would improve the readers' experience to prevent all these warnings. Please let me know!

@carols10cents
Copy link
Member

No, I think it would be distracting.

@MarkDBlackwell
Copy link

The emitted warnings would be better addressed by mdBook—I found a relevant issue.

@HenrikBach1
Copy link
Author

Hi,

The problem is not with the "dead" code and elimination of it. The problem is that the compiler emits, in my opion, an error error that is not true, due to that the field of the struct is used later in the code.

Maybe, this wasn't the right forum to discuss this issue in...?

@HenrikBach1
Copy link
Author

Above issue is now reported in: rust-lang/rust#95105

Thank you for your response.

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

3 participants