Tuple struct constructor complains about private fields

泪湿孤枕 提交于 2019-11-29 05:35:48

As the error message suggests, the problem is JobsList has a private field, that is, the Vec<Job> value is inaccessible outside the module that defines the struct. This means that you cannot pattern match on a JobsList value to extract it, and that you cannot construct it directly.

There's two fixes:

  • make the field public pub struct JobsList(pub Vec<Job>);
  • provide a public constructor

    impl JobsList {
        pub fn new(jobs: Vec<Job>) -> JobsList {
            JobsList(jobs)
        }
    }
    

    called like JobsList::new(vec![]).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!