Tuple struct constructor complains about private fields

后端 未结 1 2014
旧巷少年郎
旧巷少年郎 2020-12-16 11:14

I am working on a basic shell interpreter to familiarize myself with Rust. While working on the table for storing suspended jobs in the shell, I have gotten stuck at the fol

相关标签:
1条回答
  • 2020-12-16 11:40

    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![]).

    0 讨论(0)
提交回复
热议问题