在src/lib.rs我有以下
extern crate opal_core;
mod functions;
mod context;
mod shader;
Run Code Online (Sandbox Code Playgroud)
然后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: …Run Code Online (Sandbox Code Playgroud) 我有一个货物项目包括三个文件在同一目录下:main.rs,mod1.rs和mod2.rs.
我想从导入功能mod2.rs,以mod1.rs同样的方式,我会从导入功能mod1.rs来main.rs.
我已经阅读了所需的文件结构但我没有得到它 - 命名所有导入的文件mod将导致编辑器中的轻微混淆,这也只是使项目层次结构复杂化.
有没有像在Python或C++中那样独立于目录结构导入/包含文件的方法?
main.rs:
mod mod1; // Works
fn main() {
println!("Hello, world!");
mod1::mod1fn();
}
Run Code Online (Sandbox Code Playgroud)
mod1.rs:
mod mod2; // Fails
pub fn mod1fn() {
println!("1");
mod2::mod2fn();
}
Run Code Online (Sandbox Code Playgroud)
mod2.rs:
pub fn mod2fn() {
println!("2");
}
Run Code Online (Sandbox Code Playgroud)
建立结果:
error: cannot declare a new module at this location
--> src\mod1.rs:1:5
|
1 | mod mod2;
| ^^^^
|
note: maybe move this module `src` to …Run Code Online (Sandbox Code Playgroud) 我的目录结构:
src
main.rs
image.rs
decoders.rs
Run Code Online (Sandbox Code Playgroud)
当我尝试在 image.rs 中导入我的解码器模块时,我得到了这个:
error[E0583]: File not found for module `decoders`
Run Code Online (Sandbox Code Playgroud)
解码器.rs:
pub mod Decoders {}
Run Code Online (Sandbox Code Playgroud)
图像.rs:
mod decoders
use decoders::Decoders
pub mod Image {}
Run Code Online (Sandbox Code Playgroud)
注意:我正在使用一个专门包装整个文件的模块,这样我就可以将属性放在整个文件上。这就是为什么它不是How to include module from another file from the same project?
奇怪的是,当我尝试在 main.rs 中导入 Image 时,这种语法非常有效:
mod image;
use image::Image;
Run Code Online (Sandbox Code Playgroud)