]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0451.md
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0451.md
1 A struct constructor with private fields was invoked.
2
3 Erroneous code example:
4
5 ```compile_fail,E0451
6 mod Bar {
7     pub struct Foo {
8         pub a: isize,
9         b: isize,
10     }
11 }
12
13 let f = Bar::Foo{ a: 0, b: 0 }; // error: field `b` of struct `Bar::Foo`
14                                 //        is private
15 ```
16
17 To fix this error, please ensure that all the fields of the struct are public,
18 or implement a function for easy instantiation. Examples:
19
20 ```
21 mod Bar {
22     pub struct Foo {
23         pub a: isize,
24         pub b: isize, // we set `b` field public
25     }
26 }
27
28 let f = Bar::Foo{ a: 0, b: 0 }; // ok!
29 ```
30
31 Or:
32
33 ```
34 mod Bar {
35     pub struct Foo {
36         pub a: isize,
37         b: isize, // still private
38     }
39
40     impl Foo {
41         pub fn new() -> Foo { // we create a method to instantiate `Foo`
42             Foo { a: 0, b: 0 }
43         }
44     }
45 }
46
47 let f = Bar::Foo::new(); // ok!
48 ```