Warning says struct not constructed

struct Output;

impl Output {
    fn new() -> Self {
        Self
    }

    fn clear_screen() -> crossterm::Result<()> {
        execute!(stdout(), terminal::Clear(ClearType::All))
    }

    fn refresh_screen(&self) -> crossterm::Result<()> {
        Self::clear_screen()
    }
}

looks good to me.

The concept of "used" that the unused item warnings use is transitive, in that they only consider things "used" if they are used by things that are themselves used (or items accessible from outside the crate, and fn main); if you never call Output::new, then you never construct Output, so you will get a warning for both.

4 Likes

you mean its associated function ?

Please stop posting in all bold: it's wholly unnecessary.

No instance of Output is ever actually constructed in any possible execution path. As such, the lint is triggered.

1 Like

The example compiles to a no-op because there is no main and no public data structures or methods.

Try

pub struct Output;

impl Output {
    pub fn new() -> Self {
        Self
    }

    pub fn clear_screen() -> crossterm::Result<()> {
        execute!(stdout(), terminal::Clear(ClearType::All))
    }

    pub fn refresh_screen(&self) -> crossterm::Result<()> {
        Self::clear_screen()
    }
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.