如何从兄弟模块导入?

65
src/lib.rs 文件中,我有以下内容。
extern crate opal_core;

mod functions;
mod context;
mod shader;

然后在src/context.rs中,我有类似这样的内容,试图从src/shader.rs导入符号:

use opal_core::shader::Stage;
use opal_core::shader::Shader as ShaderTrait;
use opal_core::GraphicsContext as GraphicsContextTrait;

use functions::*; // this import works fine
use shader::*; // this one doesn't

pub struct GraphicsContext {
    functions: Gl
}

fn shader_stage_to_int(stage: &Stage) -> u32 {
    match stage {
        &Stage::Vertex => VERTEX_SHADER,
        &Stage::Geometry => GEOMETRY_SHADER,
        &Stage::Fragment => FRAGMENT_SHADER,
    }
}

impl GraphicsContextTrait for GraphicsContext {

    /// Creates a shader object
    fn create_shader(&self, stage: Stage, source: &str) -> Box<ShaderTrait> {
        let id;

        unsafe {
            id = self.functions.CreateShader(shader_stage_to_int(&stage));
        }

        let shader = Shader {
            id: id,
            stage: stage,
            context: self
        };

        Box::new(shader)
    }
}

问题在于语句use shader::*;报错,显示为unresolved import
我阅读了文档,文档中说use语句总是从当前crate的根目录(opal_driver_gl)开始,所以我认为shader::*应该导入opal_driver_gl::shader::*,但实际上并没有这样做。我需要在这里使用selfsuper关键字吗?

你有看过任何提到相同错误的其他问题吗?如果有,你的问题与它们有何不同?你尝试过制作一个更小的测试用例吗? - Shepmaster
1
我已经查看了大部分“未解决的导入”问题。它们大多集中在从一个箱外获取符号,但我想做相反的事情。我会尝试缩小问题范围。 - neon64
1
告诉我们你尝试了什么,看到了什么问题以及为什么这些尝试和问题不起作用或者你从中没有理解的内容是一个好的实践。这可以防止我们猜测你真正的问题,使你更容易得到答案,并且一般来说,可以提高你的问题对未来搜索者有多大的用处。 - Shepmaster
1
我看不出明显的原因,但是... shader 中是否包含来自 context 的任何导入?全局导入可能会导致模块递归依赖于自身而出现问题。尝试移除 * 并列出您实际使用的所有符号。 - DK.
1
你的着色器模块是否有任何公共项? - llogiq
2个回答

55

请注意,use 的行为从 Rust 2015 到 Rust 2018 发生了变化。有关详细信息,请参见“use”关键字中有效路径根是什么?

Rust 2018

要导入同级别的模块,请执行以下操作:

random_file_0.rs

// Note how this is a public function. It has to be in order to be
// usable from other files (in this case `random_file_1.rs`)
pub fn do_something() -> bool {
    true
}

random_file_1.rs

use super::random_file_0;

#[test]
fn do_something_else() {
    assert!(random_file_0::do_something());
}

或者一个备选的random_file_1.rs

use crate::random_file_0;

#[test]
fn do_something_else() {
    assert!(random_file_0::do_something());
}

lib.rs

mod random_file_0;
mod random_file_1;

查看Rust By Example获取更多信息和例子。如果不起作用,请参见它显示的代码:

fn function() {
    println!("called `function()`");
}

mod cool {
    pub fn function() {
        println!("called `cool::function()`");
    }
}

mod my {
    fn function() {
        println!("called `my::function()`");
    }

    mod cool {
        pub fn function() {
            println!("called `my::cool::function()`");
        }
    }

    pub fn indirect_call() {
        // Let's access all the functions named `function` from this scope!
        print!("called `my::indirect_call()`, that\n> ");

        // The `self` keyword refers to the current module scope - in this case `my`.
        // Calling `self::function()` and calling `function()` directly both give
        // the same result, because they refer to the same function.
        self::function();
        function();

        // We can also use `self` to access another module inside `my`:
        self::cool::function();

        // The `super` keyword refers to the parent scope (outside the `my` module).
        super::function();

        // This will bind to the `cool::function` in the *crate* scope.
        // In this case the crate scope is the outermost scope.
        {
            use cool::function as root_function;
            root_function();
        }
    }
}

fn main() {
    my::indirect_call();
}

Rust 2015

要导入同级模块,请执行以下操作:

random_file_0.rs

// Note how this is a public function. It has to be in order to be
// usable from other files (in this case `random_file_1.rs`)
pub fn do_something() -> bool {
    true
}

random_file_1.rs:

use super::random_file_0;

#[test]
fn do_something_else() {
    assert!(random_file_0::do_something());
}

或者另一种选择是random_file_1.rs

use ::random_file_0;

#[test]
fn do_something_else() {
    assert!(random_file_0::do_something());
}

lib.rs:

mod random_file_0;
mod random_file_1;

以下是来自 Rust By Example 先前版本的另一个示例:

fn function() {
    println!("called `function()`");
}

mod my {
    pub fn indirect_call() {
        // Let's access all the functions named `function` from this scope
        print!("called `my::indirect_call()`, that\n> ");

        // `my::function` can be called directly
        function();

        {
            // This will bind to the `cool::function` in the *crate* scope
            // In this case the crate scope is the outermost scope
            use cool::function as root_cool_function;

            print!("> ");
            root_cool_function();
        }

        {
            // `self` refers to the current module scope, in this case: `my`
            use self::cool::function as my_cool_function;

            print!("> ");
            my_cool_function();
        }

        {
            // `super` refers to the parent scope, i.e. outside of the `my`
            // module
            use super::function as root_function;

            print!("> ");
            root_function();
        }
    }

    fn function() {
        println!("called `my::function()`");
    }

    mod cool {
        pub fn function() {
            println!("called `my::cool::function()`");
        }
    }
}

mod cool {
    pub fn function() {
        println!("called `cool::function()`");
    }
}

fn main() {
    my::indirect_call();
}

感谢提供的信息,不过很遗憾,我已经了解基础知识。@DK认为他找到了问题所在,因为我使用了循环全局导入。(我之前是从Java世界来的,在那里使用import myPackage.*;是可以的) - neon64
我的问题类似,但我认为你没有正确回答哈里森...实际上,我无法做到这一点:https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e51977fc203b23a39793816253d1aa43 - Cirelli94
4
在我的实际问题中,“my”和“cool”是两个不同的文件,因此有两个模块。 但是在我的代码中,我无法使用super::或crate!来调用cool模块。 - Cirelli94

0
如何在另一个文件中使用来自file1的函数:

file1.rs

pub fn app() {  // pub is necessary
    println("hi");
}

main.rs

// declare module you want to use here
// only main.rs and lib.rs can contain root module declarations, from other files, it becomes submodules

mod file1; 
fn main() {
    file1::app();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接