Tuple struct constructor complains about private fields

14,995

Solution 1

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

Solution 2

The same error can be caused by using a Box pointer with incorrect syntax.

    let list_node_5 = ListNode::new(5, None);
    
    //CORRECT SYNTAX: 
    let list_node_4 = ListNode::new(4, Some(Box::new(list_node_5)));
    
    //INCORRECT SYNTAX: 
    let list_node_4 = ListNode::new(4, Some(Box(list_node_5));

Complete context: manually building a linked list for testing a LeetCode problem. https://leetcode.com/problems/remove-nth-node-from-end-of-list/


    pub struct ListNode {
      val: i32,
      next: Option<Box<ListNode>>,
    }
    
    impl ListNode {
      pub fn new(val: i32, next: Option<Box<ListNode>>) -> Self {
        ListNode {
          val,
          next,
        }
      }
      //[...]
    }
    
    fn main() {
      let list_node_5 = ListNode::new(5, None);
      let list_node_4 = ListNode::new(4, Some(Box::new(list_node_5)));
      let list_node_4 = ListNode::new(4, Some(Box(list_node_5));
      
      //[...]
      
    }

Share:
14,995

Related videos on Youtube

Grubermensch
Author by

Grubermensch

Notables: Software Engineer at Cisco Meraki. Former President of the CMU Computer Club and Head Organizer of the Demosplash demoparty. BCSA in Computer Science and Architectural Design (2014) from Carnegie Mellon University. Interests: Human-Computer Interaction Futurism Worldbuilding Philosophy Game Design

Updated on January 03, 2022

Comments

  • Grubermensch
    Grubermensch over 2 years

    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 following compiler error message:

    error: cannot invoke tuple struct constructor with private fields [E0450]
         let jobs = job::JobsList(vec![]);
                    ^~~~~~~~~~~~~
    

    It's unclear to me what is being seen as private here. As you can see below, both of the structs are tagged with pub in my module file. So, what's the secret sauce?

    mod job {
        use std::fmt;
    
        pub struct Job {
            jid: isize,
            pid: isize,
            cmd: String,
        }
    
        pub struct JobsList(Vec<Job>);
    }
    
    fn main() {
        let jobs = job::JobsList(vec![]);
    }