-1

I'm trying to use a struct from a value.rs file from chunk.rs, but it is not working. This is my current file hierarchy.

src
|
|---value.rs
|---chunk.rs
|---other files

Here is my value.rs code:

pub enum Value {
}

pub struct ValueArray {
    values: Vec<Value>,
}

pub impl Value {
    pub fn new() -> Value {
        Value {
            values: Vec::new(),
        }
    }

    pub fn write_chunk(&mut self, value: Value) {
        self.code.push(value);
    }
}

Here is my chunk.rs code:

use crate::value::ValueArray; // Error Here

pub enum OpCode {
    OpReturn,
}

pub struct Chunk {
    pub code: Vec<OpCode>,
    constants: value::ValueArray,
}

impl Chunk {
    pub fn new() -> Chunk {
        Chunk {
            code: Vec::new(),
            constants: value::ValueArray::new(),
        }
    }

    pub fn write_chunk(&mut self, byte: OpCode) {
        self.code.push(byte);
    }
}

This is my exact error message:

unresolved import `crate::value`
could not find `value` in the crate rootrustc(E0432)
chunk.rs(1, 12): could not find `value` in the crate root

I'm not sure why it isn't working since I've done something very similar in another sibling file. I'm new to Rust, so I appreciate all of your help. Thanks

Herohtar
  • 5,347
  • 4
  • 31
  • 41
  • 1
    Does this answer your question? [How to use one module from another module in a Rust cargo project?](https://stackoverflow.com/questions/48071513/how-to-use-one-module-from-another-module-in-a-rust-cargo-project) – Jmb Oct 18 '21 at 07:06
  • Yes, this helps me with additional information. Thanks –  Oct 18 '21 at 11:30

2 Answers2

3

You need to define the value module on the lib.rs file

pub mod value;
1

The syntax I'm using to include a file ("lib.rs") with naked structs/functions (no explicit module defined) is:

mod lib;
use crate::lib::*;

So you must be missing mod value;.

piojo
  • 6,351
  • 1
  • 26
  • 36