【问题标题】:How do I import from a sibling module?如何从同级模块导入?
【发布时间】:2015-08-21 00:53:44
【问题描述】:

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::*; 给出了错误未解决的导入

我正在阅读文档,他们说 use 语句总是从当前板条箱的根 (opal_driver_gl) 开始,所以我认为 shader::* 应该导入 opal_driver_gl::shader::* 但它似乎没有这样做所以。我需要在这里使用selfsuper 关键字吗?

如果您能提供帮助,谢谢。

【问题讨论】:

  • 你看过other questions that mention the same error吗?如果是这样,您的问题与他们有何不同?你试过smaller testcase吗?
  • 我已经检查了大部分“未解决的导入”问题。他们主要集中在从板条箱外面获取符号,但我想做相反的事情。我会尝试缩小问题范围。
  • 告诉我们您尝试过的内容以及遇到的问题被认为是一种很好的做法。还包括为什么这些尝试和问题不起作用,或者你不明白什么。这可以防止我们猜测您的真正问题是什么,让您更容易获得答案,并且通常会提高您的问题对未来搜索者的有用程度。
  • 我看不到任何明显的原因,但是...shader 是否包含来自context 的任何导入?如果您最终得到一个依赖于自身的模块,则全局导入可能会导致问题。尝试删除 * 并列出您实际使用的所有符号。
  • 另外你的shader模块真的有公共项吗?

标签: import rust


【解决方案1】:

请注意,use 的行为已从 Rust 2015 更改为 Rust 2018。有关详细信息,请参阅 What are the valid path roots in the use keyword?

生锈 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();
}

生锈 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.*; 很好)
  • 我的问题很相似,但我认为你的回答不正确,哈里森......事实上我不能这样做:play.rust-lang.org/…
  • 在我的真正问题中,“我的”和“酷”是两个不同的文件,所以两个模块。但在我的情况下,我不记得 super:: 和 crate 的酷!
猜你喜欢
  • 2019-06-19
  • 1970-01-01
  • 2014-05-02
  • 2015-12-23
  • 1970-01-01
  • 1970-01-01
  • 2022-07-22
  • 2021-11-08
  • 2015-07-26
相关资源
最近更新 更多