27

I am trying to split my project into multiple files but I am having problems importing them into my main.rs as it says the Column's fields are private but I have declared the struct as public.

src/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 build

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]}; 

2 Answers 2

45

Try labeling the fields as public:

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

Labeling Column as pub means that other modules can use the struct itself, but not necessarily all of its members.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahhh, ok. I assumed that using pub struct would make the struct's fields as that's the impression I got from the book.
23

You've declared the struct as public, but not the fields. To make both fields public, the struct declaration should look as follows:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.