简体   繁体   English

如何从同级模块导入?

[英]How do I import from a sibling module?

In src/lib.rs I have the followingsrc/lib.rs我有以下

extern crate opal_core;

mod functions;
mod context;
mod shader;

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

The problem is that the statement use shader::*;问题是该语句use shader::*; gives the error unresolved import .给出错误unresolved import

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.我正在阅读文档,他们说use语句总是 go 从当前板条箱( opal_driver_gl )的根目录开始,所以我认为shader::*应该导入opal_driver_gl::shader::*但它似乎没有这样做。 Do I need to use the self or super keywords here?我需要在这里使用selfsuper关键字吗?

Thanks if you can help.谢谢,如果你能帮忙。

Note that the behavior of use has changed from Rust 2015 to Rust 2018. See What are the valid path roots in the use keyword?需要注意的是行为use已经从2015年锈变更为2018年锈病见什么都在使用关键字的有效途径根源是什么? for details.详情。

Rust 2018铁锈 2018

To import a module on the same level, do the following:要在同一级别导入模块,请执行以下操作:

random_file_0.rs 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 random_file_1.rs

use super::random_file_0;

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

or an alternative random_file_1.rs :或替代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;

SeeRust By Example for more information and examples.有关更多信息和示例,请参阅Rust By Example If that doesn't work, here is the code it shows:如果这不起作用,这是它显示的代码:

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生锈 2015

To import a module on the same level, do the following:要在同一级别导入模块,请执行以下操作:

random_file_0.rs : 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 : random_file_1.rs

use super::random_file_0;

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

or an alternative random_file_1.rs :或替代random_file_1.rs

use ::random_file_0;

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

lib.rs : lib.rs :

mod random_file_0;
mod random_file_1;

Here is another example from a previous version of Rust By Example:以下是 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();
}

Importing modules from directories/sub-directories is a little tricky actually.从目录/子目录导入模块实际上有点棘手。 Spent 3 hours today on understanding this entirely as the documentation lacks for this one, otherwise Rust's docs are damn good.今天花了 3 个小时来完全理解这一点,因为该文档缺少文档,否则 Rust 的文档非常好。 I've created a sample project that shows how to do it.我创建了一个示例项目来展示如何做到这一点。 https://github.com/amitstefen/rust_modules_example https://github.com/amitstefen/rust_modules_example

These two files are the key-这两个文件是关键-

在此处输入图像描述

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

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