The struct field is private when importing a module

I am trying to split my project into several files, but I am having problems importing them into my main.rs , as it says the column fields are private, but I declared the structure open.

SIC / column.rs

 pub struct Column { name: String, vec: Vec<i32>, } 

Src / main.rs

 pub mod column; fn main() { let col = column::Column{name:"a".to_string(), vec:vec![1;10]}; println!("Hello, world!"); } 

cargo assembly

 src/main.rs:4:15: 4:75 error: field `name` of struct `column::Column` is private src/main.rs:4 let col = column::Column{name:"a".to_string(), vec:vec![1;10]}; ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src/main.rs:4:15: 4:75 error: field `vec` of struct `column::Column` is private src/main.rs:4 let col = column::Column{name:"a".to_string(), vec:vec![1;10]}; 
+6
source share
2 answers

Try marking the fields as public:

 pub struct Column { pub name: String, pub vec: Vec<i32>, } 

Marking Column as pub means that other modules can use the structure itself, but not all of its elements.

+15
source

You declared the structure open, but not fields. To make both fields public, the structure declaration should look like this:

 pub struct Column { pub name: String, pub vec: Vec<i32>, } 
+10
source

Source: https://habr.com/ru/post/984030/


All Articles