Why i can't get access to private fields in member functions?

#[derive(Debug)]
pub struct nav2_filesys {
    pub path : String,
    pub mode : nav2_filemode,
    pub filetype : nav2_filetype,
    options : OpenOptions,
    file_handle : Option<File>,
    textreader : Option<BufReader<File>>,
}
 impl nav2_filesys {
        // # new函数,只需添加要读取(写入)文件的路径
        pub fn new(filepath : String) -> nav2_filesys {
            nav2_filesys {
                path : filepath.clone(),
                mode : nav2_filemode::reader,
                filetype : nav2_filetype::text,
                options : OpenOptions::new(),
                file_handle : None,
                textreader : None,
            }
        }

        // # 设置读取模式
        pub fn set_mode(&mut self, mode : nav2_filemode) {
            self.mode = mode;
            match mode {
                nav2_filemode::reader => self.options.read(true),
                nav2_filemode::writer => self.options.write(true).create(true),
                nav2_filemode::appender => self.options.append(true),
            };
        }

        // # 设置文档编码格式
        pub fn set_format(&mut self, format : nav2_filetype) {
            self.filetype = format;
            // 当读取格式为文本时,需要将File绑定给
            if self.filetype == nav2_filetype::text {

            }
        }


        // # 打开文件
        pub fn open(&mut self) -> std::io::Result<()> {
            self.file_handle = Some(self.options.open(self.path.as_str()));
                // match self.mode {
                //     nav2_filemode::reader => self.options.open(self.path.as_str())?,
                //     nav2_filemode::writer => self.options.open(self.path.as_str())?,
                //     nav2_filemode::appender => self.options.open(self.path.as_str())?,
                // }
            Ok(())
        }

        // # 读取文本文件的一行
        pub fn read_line(&mut self) -> std::io::Result<String> {
            Ok(String::new())
        }

    }

the error messages ars as follows :

error[E0616]: field `options` of struct `io_type::nav2_filesys` is private
  --> src/nav2_io_impl.rs:27:47
   |
27 |                 nav2_filemode::reader => self.options.read(true),
   |                                               ^^^^^^^ private field

error[E0616]: field `options` of struct `io_type::nav2_filesys` is private
  --> src/nav2_io_impl.rs:28:47
   |
28 |                 nav2_filemode::writer => self.options.write(true).create(true),
   |                                               ^^^^^^^ private field

error[E0616]: field `options` of struct `io_type::nav2_filesys` is private
  --> src/nav2_io_impl.rs:29:49
   |
29 |                 nav2_filemode::appender => self.options.append(true),
   |                                                 ^^^^^^^ private field

error[E0616]: field `file_handle` of struct `io_type::nav2_filesys` is private
  --> src/nav2_io_impl.rs:45:18
   |
45 |             self.file_handle = Some(self.options.open(self.path.as_str()));
   |                  ^^^^^^^^^^^ private field

error[E0616]: field `options` of struct `io_type::nav2_filesys` is private
  --> src/nav2_io_impl.rs:45:42
   |
45 |             self.file_handle = Some(self.options.open(self.path.as_str()));
   |                                          ^^^^^^^ private field

For more information about this error, try `rustc --explain E0616`.

i'm a new rust programer ,thank you very much if could help :wink:

The formatting is messed up so I'll make a guess: there are multiple modules involved. Access control in Rust is based on which module a function or method lives in, not by what type a method is defined for.

1 Like

This is the problem.

The inherent impl blocks must be in the same file, or a submodule, of the struct definition.

Try moving the file to src/io_type/nav2_impl.rs

And writing struct nav2_filesys{.....}; mod nav2_impl;

1 Like

What's visible in an impl is only determined by what's visible from the module the impl is defined in.

For private fields, this means, as @riking explained, that the impl must be in the same module or a nested module. Alternatively, you can also adjust the visibility and make the fields slightly more public with things like pub(super) or pub(in path…). See the reference for some examples of how those work: Visibility and privacy - The Rust Reference

1 Like

thanks, I still use the organization of files in c/c++ programming which caused the problem.

thanks, I still use the organization of files in c/c++ programming which caused the problem. Problems solved :wink:

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.