2

I am a newbie at rust programming. Here I was developing a FLTK app using rust. I am getting the following warning.

warning: field is never read: `group`
--> src\views\logo.rs:13:5
   |
13 |     group: fltk::group::Group,
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

Why is it showing the warning: field is never read: group? Is there some option resolve this warning? my code is this:

use fltk::{image::*,
    frame::Frame,
    group::Group,
    enums::Color,
    // enums::Align,
};
use fltk::prelude::*;
use std::fs::read;


pub struct LogoImage {
    group: fltk::group::Group,
    // // button: fltk::button::Button,
    // logo_frame: Frame,
}

impl LogoImage {
    pub fn new(x:i32, y:i32, w:i32, h:i32, label:&'static str) -> LogoImage {
        let mut group = Group::new(x, y, w, h, label);
        group.set_align(fltk::enums::Align::Center);
        group.set_frame(fltk::enums::FrameType::FlatBox);
        group.set_color(fltk::enums::Color::Red);
        // group.set_type(fltk::group::PackType::Horizontal);

        let mut logo_frame = Frame::default()
        // .with_size(w, h)
        .with_size(group.width(), group.height())
        .center_of(&group)
        .with_label("H");
        logo_frame.set_frame(fltk::enums::FrameType::FlatBox);
        logo_frame.set_color(Color::Yellow);

        logo_frame.draw(|f| {
            // let image_data = read("assets\\app_icon.png").unwrap();
            let mut img = PngImage::load("assets\\app_icon.png").unwrap();
            img.scale(f.w(), f.h(), true, true);
            img.draw(f.x(), f.y(), f.w(), f.h());
        });
        
        group.end();
        LogoImage {group} // , logo_frame
    }
    
}

Can some explain me this and give a solution as well (prefer solution in code not in just explanation)?

kmdreko
  • 42,554
  • 6
  • 57
  • 106
Santo K. Thomas
  • 153
  • 2
  • 12
  • 3
    That field is not `pub`, and there is no `pub` method reading from it. Thus the compiler reasons that you may well omit that field together with statements writing to it. One can always precede the field name with an underscore to inform the compiler that it might not be used, and suppress this warning. – Ruifeng Xie May 18 '21 at 17:37
  • if `pub` is used before `group: fltk::group::Group`, will that warning go away? The struct is already a `pub` one right? Then why again a `pub` needed inside? – Santo K. Thomas May 18 '21 at 17:43
  • 2
    There can be many reasons to hide the internals of structs. For example the consistency may be broken when you change some field from outside. That's why there's a visibility modifier on struct fields. – Denys Séguret May 18 '21 at 17:44
  • @SantoK.Thomas `struct` fields (when not annotated with `pub`) are by default private to the module in which it is defined. And you don’t have a function reading from it in the same module (which is the scope where that field is publicly available). So it is considered never read. – Ruifeng Xie May 18 '21 at 17:46
  • @RuifengXie So, `group: fltk::group::Group` should be changed to `pub group: fltk::group::Group`. Am I right? – Santo K. Thomas May 18 '21 at 17:52
  • 2
    @SantoK.Thomas I believe so. Or you can rename that field to `_group` (and thus no need of a `pub`) so that the warning is suppressed and field accessibility is not affected. – Ruifeng Xie May 18 '21 at 17:54
  • Thank you very much DenysSéguret & RuifengXie – Santo K. Thomas May 18 '21 at 17:58

1 Answers1

-1

Make the internals (internal variables) of the struct either intentionally starting with _ (ie., _group) to avoid warning or use pub in front of the internal variable (ie., pub group ...) and use it in another rs file. Answer:


    use fltk::{image::*,
        frame::Frame,
        group::Group,
        enums::Color,
        // enums::Align,
    };
    use fltk::prelude::*;
    use std::fs::read;
    
    
    pub struct LogoImage {
        _group: fltk::group::Group, // or pub group: .......
    }
    
    impl LogoImage {
        pub fn new(x:i32, y:i32, w:i32, h:i32, label:&'static str) -> LogoImage {
            let mut group = Group::new(x, y, w, h, label);
            group.set_align(fltk::enums::Align::Center);
            group.set_frame(fltk::enums::FrameType::FlatBox);
            group.set_color(fltk::enums::Color::Red);
            // group.set_type(fltk::group::PackType::Horizontal);
    
            let mut logo_frame = Frame::default()
            // .with_size(w, h)
            .with_size(group.width(), group.height())
            .center_of(&group)
            .with_label("H");
            logo_frame.set_frame(fltk::enums::FrameType::FlatBox);
            logo_frame.set_color(Color::Yellow);
    
            logo_frame.draw(|f| {
                // let image_data = read("assets\\app_icon.png").unwrap();
                let mut img = PngImage::load("assets\\app_icon.png").unwrap();
                img.scale(f.w(), f.h(), true, true);
                img.draw(f.x(), f.y(), f.w(), f.h());
            });
            
            group.end();
            LogoImage {group} // , logo_frame
        }
        
    }

Jasha
  • 5,507
  • 2
  • 33
  • 44
Santo K. Thomas
  • 153
  • 2
  • 12