Closed
Description
mod first {
#[derive(Debug)]
pub struct SomeId(u32);
}
mod second {
use ::first::SomeId;
#[derive(Debug)]
pub enum MyEnum {
Variant1 {
id: SomeId
}
}
}
fn main() {
use first::SomeId;
use second::MyEnum;
let test = MyEnum::Variant1 {
id: SomeId(32),
};
println!("{:?}", test);
}
This gave me the following error message:
error[E0423]: expected function, found struct `SomeId`
--> src/main.rs:22:13
|
22 | id: SomeId(32),
| ^^^^^^
| |
| did you mean `Some`?
| constructor is not visible here due to private fields
help: possible better candidate is found in another module, you can import it into scope
|
1 | use first::SomeId;
|
It turns out the fix for this was to change my struct definition to pub struct SomeId(pub u32);
. This was not obvious to me for several reasons:
- The
expected function, found struct SomeId
is odd because that's what I'm trying to perform, so it appears like it's erroring because it's doing what I want. - The suggestion for the use statement is completely incorrect, as that use statement already exists and won't fix the problem
- The
constructor is not visible here due to private fields
didn't make sense to me as the rust book never gives an example of theStruct(pub fieldType)
syntax, so I assumed tuple fields had the same visibility as the struct itself.
It probably needs a recommendation for tuple structs to show that all fields should be marked as pub