Home > Blockchain >  How can I use a class (struct impl) that's found in another class
How can I use a class (struct impl) that's found in another class

Time:09-30

I want to use a class that's created inside another class in my main method. The Packagemnm class has a vector of Mnm classes and you can add to them using the add() method which takes in a Mnm object and adds it to the vector. When I try to compile this cargo gives the following error:

   Compiling candy v0.1.0 (/mnt/c/Users/mateo/Coding/candy)
error[E0433]: failed to resolve: use of undeclared type `Mnm`
  --> src/main.rs:8:14
   |
8  |     pack.add(Mnm::new());
   |              ^^^ not found in this scope
   |
note: struct `crate::packagemnm::mnm::Mnm` exists but is inaccessible
  --> src/packagemnm/mnm.rs:12:1
   |
12 | pub struct Mnm {
   | ^^^^^^^^^^^^^^ not accessible

For more information about this error, try `rustc --explain E0433`.
error: could not compile `candy` due to previous error

I don't really understand it because the Mnm struct is already included inside the mod.rs file. I tried including it but the compiler told me that it was inaccessible.

My src directory looks like this where the mod.rs file contains the Packagemnm class

src/
├── main.rs
└── packagemnm
    ├── mnm.rs
    └── mod.rs

main.rs:

mod packagemnm;

fn main() {

    // let red = mnm::Mnm::new(mnm::Colors::RED);
    let pack = packagemnm::Packagemnm::new();

    pack.add(Mnm::new());
}

packagemnm/mod.rs

mod mnm;
use mnm::{Colors, Mnm};

pub struct Packagemnm {
    m_list: Vec<Mnm>,

    red: i32,
    blue: i32,
    green: i32,
    brown: i32,
    yellow: i32,
    orange: i32
}

impl Packagemnm {
    pub fn new() -> Packagemnm {
        Packagemnm {
            m_list: Vec::new(),

            red: 0,
            blue: 0,
            green: 0,
            brown: 0,
            yellow: 0,
            orange: 0
        }
    }

    pub fn add(&mut self, obj: Mnm) {
        self.m_list.push(obj);

        match obj.get_color() {
            Colors::RED => self.red =1,
            Colors::BLUE => self.blue =1,
            Colors::GREEN => self.green =1,
            Colors::YELLOW => self.yellow =1,
            Colors::ORANGE => self.orange =1,
            Colors::BROWN => self.brown =1,
            _ => ()
        }
    }

    pub fn to_string(&mut self) -> String {
        let s: String = format!("Red: {}\nBlue: {}\nGreen: {}\nBrown: {}\nYellow: {}\nOrange: {}",
                self.red, self.blue, self.green, self.brown, self.yellow, self.orange);
        s
    }
}

packagemnm/mnm.rs

#[derive(Copy, Clone)]
pub enum Colors {
    RED = 0,
    BLUE,
    GREEN,
    BROWN,
    YELLOW,
    ORANGE
}

#[derive(Copy, Clone)]
pub struct Mnm {
    m_color: Colors
}

impl Mnm {
    pub fn new(c: Colors) -> Mnm {
        Mnm {
            m_color: c
        }
    }

    pub fn get_color(&self) -> Colors {
        self.m_color
    }
}

CodePudding user response:

I believe the the mod keyword in rust only declares the additional file; you still need to actually import the functions from that file! Try something like:

mod packagemnm;

use packagemnm::mnm::*;

fn main() {

    // let red = mnm::Mnm::new(mnm::Colors::RED);
    let pack = packagemnm::Packagemnm::new();

    pack.add(Mnm::new());
}

and then change the mod mnm declaration in mod.rs to

pub mod mnm;
  • Related