简体   繁体   English

'use' 关键字不起作用 Rust(从兄弟文件调用)

[英]'use' keyword not working Rust (calling from sibling file)

I'm trying to use a struct from a value.rs file from chunk.rs , but it is not working.我试图use一个结构从value.rs从文件chunk.rs ,但它不工作。 This is my current file hierarchy.这是我当前的文件层次结构。

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

Here is my value.rs code:这是我的value.rs代码:

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:这是我的chunk.rs代码:

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.我是 Rust 的新手,所以我感谢你的所有帮助。 Thanks谢谢

您需要在lib.rs文件上定义value模块

pub mod value;

The syntax I'm using to include a file ("lib.rs") with naked structs/functions (no explicit module defined) is:我用来包含带有裸结构/函数(未定义显式模块)的文件(“lib.rs”)的语法是:

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

So you must be missing mod value;所以你一定是缺少mod value; . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM