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:
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.constructor is not visible here due to private fields didn't make sense to me as the rust book never gives an example of the Struct(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
Even more confusing when you have an empty tuple like this:
pub struct Tuple(())
The solution is of course:
pub struct Tuple(pub ())
:confused:
Current output:
error[E0423]: expected function, tuple struct or tuple variant, found struct `SomeId`
--> file17.rs:22:13
|
22 | id: SomeId(32),
| ^^^^^^
| |
| constructor is not visible here due to private fields
| help: a tuple variant with a similar name exists: `Some`
We should point at the def span of SomeId when we say constructor is not visible here due to private fields.
Most helpful comment
Even more confusing when you have an empty tuple like this:
pub struct Tuple(())The solution is of course:
pub struct Tuple(pub ()):confused: