如何从兄弟模块导入? [英] How do I import from a sibling module?

查看:112
本文介绍了如何从兄弟模块导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

src / lib.rs 我有以下

extern crate opal_core;

mod functions;
mod context;
mod shader;

然后在 src / context.rs I有这样的东西,试图从 src / shader.rs 导入符号:

Then in src/context.rs I have something like this, which tries to import symbols from 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)
    }
}

问题是声明使用shader :: *; 会给出错误 unresolved import

The problem is that the statement use shader::*; gives the error unresolved import.

我正在阅读文档,他们说使用语句总是来自当前包的根目录( opal_driver_gl )所以我认为 shader :: * 应该导入 opal_driver_gl :: shader :: * 但是它似乎没有这样做。我需要在这里使用 self super 关键字吗?

I was reading the docs and they said that use statements always go from the root of the current crate (opal_driver_gl) so I thought shader::* should be importing opal_driver_gl::shader::* but it doesn't appear to do so. Do I need to use the self or super keywords here?

谢谢你能提供帮助。

推荐答案

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

To import a module on the same level, do the following:

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

// This can be a public function, but does not have to be unless you
// are using it somewhere else
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 获取更多信息和示例。如果这不起作用,这里是它显示的代码:

See this link: Rust By Example for more information and examples. If that doesn't work, here is the code it shows:

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

这篇关于如何从兄弟模块导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆