首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从同级模块导入?

如何从同级模块导入?
EN

Stack Overflow用户
提问于 2015-06-06 07:27:16
回答 1查看 17.9K关注 0票数 53

src/lib.rs中,我有以下内容

代码语言:javascript
复制
extern crate opal_core;

mod functions;
mod context;
mod shader;

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

代码语言:javascript
复制
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语句总是从当前机箱(opal_driver_gl)的根开始,所以我认为shader::*应该导入opal_driver_gl::shader::*,但它似乎没有这样做。这里需要使用selfsuper关键字吗?

如果你能帮上忙,谢谢。

EN

回答 1

Stack Overflow用户

发布于 2015-06-07 05:37:52

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

Rust 2018

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

random_file_0.rs

代码语言:javascript
复制
// 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

代码语言:javascript
复制
use super::random_file_0;

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

或另一个random_file_1.rs

代码语言:javascript
复制
use crate::random_file_0;

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

lib.rs

代码语言:javascript
复制
mod random_file_0;
mod random_file_1;

有关更多信息和示例,请参阅Rust By Example。如果这不起作用,下面是它显示的代码:

代码语言:javascript
复制
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

代码语言:javascript
复制
// 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

代码语言:javascript
复制
use super::random_file_0;

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

或另一个random_file_1.rs

代码语言:javascript
复制
use ::random_file_0;

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

lib.rs

代码语言:javascript
复制
mod random_file_0;
mod random_file_1;

以下是Rust By example以前版本中的另一个示例:

代码语言:javascript
复制
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();
}
票数 45
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30677258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档