相关疑难解决方法(0)

如何从同一个项目的另一个文件中包含模块?

按照本指南,我创建了一个货运项目

SRC/main.rs

fn main() {
    hello::print_hello();
}

mod hello {
    pub fn print_hello() {
        println!("Hello, world!");
    }
}
Run Code Online (Sandbox Code Playgroud)

我运行使用

cargo build && cargo run
Run Code Online (Sandbox Code Playgroud)

它编译没有错误.现在我正在尝试将主模块分成两部分,但无法弄清楚如何从另一个文件中包含一个模块.

我的项目树看起来像这样

??? src
    ??? hello.rs
    ??? main.rs
Run Code Online (Sandbox Code Playgroud)

和文件的内容:

SRC/main.rs

use hello;

fn main() {
    hello::print_hello();
}
Run Code Online (Sandbox Code Playgroud)

SRC/hello.rs

mod hello {
    pub fn print_hello() {
        println!("Hello, world!");
    }
}
Run Code Online (Sandbox Code Playgroud)

当我编译它src/main.rs,我得到

error[E0432]: unresolved import `hello`
 --> src/main.rs:1:5
  |
1 | use hello;
  |     ^^^^^ no `hello` external crate
Run Code Online (Sandbox Code Playgroud)

我试图遵循编译器的建议并修改main.rs

#![feature(globs)]

extern crate hello; …
Run Code Online (Sandbox Code Playgroud)

rust

91
推荐指数
6
解决办法
5万
查看次数

如何在Rust 2015中从一个模块到另一个模块执行基本导入/包含功能?

我找不到如何将函数从一个文件(模块)包含(或导入,注入或其他一些单词)到另一个文件.

我开始一个新项目

$ cd ~/projects
$ cargo new proj --bin
$ cd proj
$ tree
.
|
-- Cargo.toml
-- src
   |
   -- main.rs
Run Code Online (Sandbox Code Playgroud)

我使用以下代码修改main.rs并创建一个新文件a.rs(在srcdir中):

main.rs

fn main() {
    println!("{}", a::foo());
}
Run Code Online (Sandbox Code Playgroud)

a.rs

pub fn foo() -> i32 { 42 }
Run Code Online (Sandbox Code Playgroud)

我运行项目cargo run并得到错误:

error[E0433]: failed to resolve: use of undeclared type or module `a`
 --> src/main.rs:2:20
  |
2 |     println!("{}", a::foo());
  |                    ^ use of undeclared type or module `a`
Run Code Online (Sandbox Code Playgroud)

似乎我需要以a …

module rust

41
推荐指数
3
解决办法
2万
查看次数

如何从兄弟模块导入?

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)

import rust

26
推荐指数
1
解决办法
6905
查看次数

如何在多个文件中使用一个文件中的函数?

我正在尝试将一个文件中的函数与多个其他文件一起使用。

当我尝试将“mod somefile”添加到文件中时,Rust 编译器希望将它们嵌套在子文件夹中,这不是我想要构建项目的方式,因为这意味着每次都会复制文件。

// src/main.rs
mod aaa;
mod bbb;

fn main() {
    aaa::do_something();
    bbb::do_something_else();
}
Run Code Online (Sandbox Code Playgroud)
// src/aaa.rs
mod zzz; // rust compiler wants the file to be nested in a subfolder as aaa/zzz.rs

pub fn do_something() {
    zzz::do_stuff();
}
Run Code Online (Sandbox Code Playgroud)
// src/bbb.rs
mod zzz; // again, compiler wants the file nested in a subfolder as bbb/zzz.rs

pub fn do_something_else() {
    zzz::do_stuff();
}
Run Code Online (Sandbox Code Playgroud)
// src/zzz.rs

pub fn do_stuff() {
    // does stuff here
}
Run Code Online (Sandbox Code Playgroud)

我希望能够保留src/zzz.rssrc文件夹并在项目中的任何其他文件中使用其功能,而不必在每个文件的子文件夹中复制它(例如:src/aaa/zzz.rs、 …

rust

2
推荐指数
1
解决办法
2164
查看次数

标签 统计

rust ×4

import ×1

module ×1