import - 如何从同级模块导入?

标签 import rust

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 关键字吗?

最佳答案

请注意,use 的行为已从 Rust 2015 更改为 Rust 2018。参见 What are the valid path roots in the use keyword?了解详情。

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

关于import - 如何从同级模块导入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30677258/

相关文章:

rust - 如何高效地将可显示项插入字符串?

rust - 如何在 Serum Anchor 中签署代币交易

inheritance - Rust 中的方法覆盖替代

database - Solr - DataImportHandler : When attempting to use column values as field names, 多值字段仅保留第一个结果

objective-c - 将实现协议(protocol)的 Swift 类导入 Objective-C 类

java - 为什么 createNewFile() 无法识别它的导入?

rust - 默认情况下是否启用可选依赖项?

rust - 由于多个可变借用,无法将插入到 trie 中的 C++ 代码移植到 Rust

node.js - 如何配置 .babelrc 以支持 ES6 模块导入和异步/等待?

python - 找不到将模块导入 python