]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0451.md
Auto merge of #106227 - bryangarza:ctfe-limit, r=oli-obk
[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 ```