Import 如何从同级模块导入?
在Import 如何从同级模块导入?,import,rust,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 Grap
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)
}
}
// 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
}
use super::random_file_0;
#[test]
fn do_something_else() {
assert!(random_file_0::do_something());
}
use ::random_file_0;
#[test]
fn do_something_else() {
assert!(random_file_0::do_something());
}
mod random_file_0;
mod random_file_1;
使用opal_core::shader::Stage;
使用opal_core::shader::shader作为ShaderTrait;
使用opal_core::GraphicsContext作为GraphicsContextTrait;
使用函数::*;//这种方法很有效
使用着色器::*;//这个没有
pub结构图形上下文{
职能:德国劳埃德船级社
}
fn着色器\u stage\u到\u int(stage:&stage)->u32{
赛段{
&阶段::顶点=>顶点着色器,
&Stage::Geometry=>Geometry\u着色器,
&Stage::Fragment=>Fragment\u着色器,
}
}
GraphicsContext的impl GraphicsContextTrait{
///创建着色器对象
fn创建_着色器(&self,stage:stage,source:&str)->框{
让我看看你的身份证;
不安全{
id=self.functions.CreateShader(shader_stage_to_int(&stage));
}
让着色器=着色器{
id:id,
阶段:阶段,,
背景:自我
};
框::新建(着色器)
}
}
问题是语句使用着色器::*代码>提供未解决的导入错误
我正在阅读文档,他们说use
语句总是从当前板条箱的根(opal\u driver\u gl
)开始,所以我认为shader:*
应该导入opal\u driver\u gl::shader:*
,但它似乎没有这样做。我需要在这里使用self
或super
关键字吗
如果你能帮忙,谢谢你 注意,使用
的行为已从Rust 2015变为Rust 2018。有关详细信息,请参阅
Rust 2018
要导入同一级别的模块,请执行以下操作:
随机文件\u 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
}
use super::random_file_0;
#[test]
fn do_something_else() {
assert!(random_file_0::do_something());
}
mod random_file_0;
mod random_file_1;
随机文件\u 1.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
}
use super::random_file_0;
#[test]
fn do_something_else() {
assert!(random_file_0::do_something());
}
mod random_file_0;
mod random_file_1;
或者另一个随机文件\u 1.rs:
use crate::random_file_0;
#[test]
fn do_something_else() {
assert!(random_file_0::do_something());
}
lib.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
}
use super::random_file_0;
#[test]
fn do_something_else() {
assert!(random_file_0::do_something());
}
mod random_file_0;
mod random_file_1;
有关更多信息和示例,请参阅。如果这不起作用,下面是它显示的代码:
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
要导入同一级别的模块,请执行以下操作:
随机文件\u 0.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)
}
}
// 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
}
use super::random_file_0;
#[test]
fn do_something_else() {
assert!(random_file_0::do_something());
}
use ::random_file_0;
#[test]
fn do_something_else() {
assert!(random_file_0::do_something());
}
mod random_file_0;
mod random_file_1;
随机文件\u 1.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)
}
}
// 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
}
use super::random_file_0;
#[test]
fn do_something_else() {
assert!(random_file_0::do_something());
}
use ::random_file_0;
#[test]
fn do_something_else() {
assert!(random_file_0::do_something());
}
mod random_file_0;
mod random_file_1;
或者另一个随机文件\u 1.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)
}
}
// 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
}
use super::random_file_0;
#[test]
fn do_something_else() {
assert!(random_file_0::do_something());
}
use ::random_file_0;
#[test]
fn do_something_else() {
assert!(random_file_0::do_something());
}
mod random_file_0;
mod random_file_1;
lib.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)
}
}
// 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
}
use super::random_file_0;
#[test]
fn do_something_else() {
assert!(random_file_0::do_something());
}
use ::random_file_0;
#[test]
fn do_something_else() {
assert!(random_file_0::do_something());
}
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();
}
你看过任何一本书吗?如果是,你的问题与他们有什么不同?你有没有试着做一个回答?我已经检查了大多数“未解决的导入”问题。它们主要围绕着从板条箱外面获取符号,但我想做的恰恰相反。我会尽量缩小问题的范围。告诉我们你做了什么,看到了什么问题,这被认为是很好的做法。还包括为什么这些尝试和问题不起作用,或者你从中不明白什么。这使我们无法猜测您真正的问题是什么,使您更容易获得答案,并总体上提高了您的问题对未来搜索者的有用程度。我看不出任何明显的原因,但。。。着色器
是否包含从上下文
导入的内容?如果您最终得到一个依赖于自身的递归模块,则Glob导入可能会导致问题。尝试删除*
并列出您实际使用的所有符号。您的着色器模块是否有任何公共项?感谢您提供的大量信息,不幸的是,我已经了解了基础知识@我认为DK在使用周期性glob进口时发现了问题。(我来自一个Java世界,导入我的包。*;
很好)我的问题与此类似,但我认为您的回答不正确Harrison。。。事实上,我不能这样做:在我真正的问题中,“my”和“cool”是两个不同的文件,所以有两个mod。但在我的记忆中,我记不起super的酷炫::板条箱也是如此!