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

时间:2014-10-06 21:11:38

标签: module rust

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

我用

开始一个新项目
$ cd ~/projects
$ cargo new proj --bin
$ cd proj
$ tree
.
|
-- Cargo.toml
-- src
   |
   -- main.rs

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

main.rs

fn main() {
    println!("{}", a::foo());
}

a.rs

pub fn foo() -> i32 { 42 }

我使用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`

似乎我需要以某种方式导入a。我尝试将以下内容添加为main.rs

的第一行
  • use a;

    error[E0432]: unresolved import `a`
     --> src/main.rs:1:5
      |
    1 | use a;
      |     ^ no `a` in the root
    
  • use a::*;

    error[E0432]: unresolved import `a`
     --> src/main.rs:1:5
      |
    1 | use a::*;
      |     ^ maybe a missing `extern crate a;`?
    
    error[E0433]: failed to resolve: use of undeclared type or module `a`
     --> src/main.rs:4:20
      |
    4 |     println!("{}", a::foo());
      |                    ^ use of undeclared type or module `a`
    
  • use a::foo;

    error[E0432]: unresolved import `a`
     --> src/main.rs:1:5
      |
    1 | use a::foo;
      |     ^ maybe a missing `extern crate a;`?
    
    error[E0433]: failed to resolve: use of undeclared type or module `a`
     --> src/main.rs:4:20
      |
    4 |     println!("{}", a::foo());
      |                    ^ use of undeclared type or module `a`
    
  • extern crate a; use a::foo;

    error[E0463]: can't find crate for `a`
     --> src/main.rs:1:1
      |
    1 | extern crate a;
      | ^^^^^^^^^^^^^^^ can't find crate
    
  • extern crate proj; use proj::a::foo;

    error[E0463]: can't find crate for `proj`
     --> src/main.rs:1:1
      |
    1 | extern crate proj;
      | ^^^^^^^^^^^^^^^^^^ can't find crate
    

我已阅读the guide,但仍无法弄清楚如何进行导入。

3 个答案:

答案 0 :(得分:38)

在mainish模块(main.rs,lib.rs或subdir / mod.rs)中,您需要为要在整个项目中使用的所有其他模块编写mod a;(或者子目录)。

在任何其他模块中,您需要撰写use a;use a::foo;

你远离唯一一个被这个混淆的人,并且它当然可以做得更好,但对模块系统的任何改变都会被拒绝,因为#34;太混乱&#34 ;

编辑:这个答案是为" Rust 2015"语言标准。对于" Rust 2018"进行了更改。标准,请参阅this blog postedition guide

答案 1 :(得分:14)

在Rust中,有一些关键字可以处理模块:

$conn->errorCode

$conn->setAttribute("PDO::ATTR_ERRMODE", PDO::ERRMODE_EXCEPTION); 填补了Cargo和Rust之间的空白。我们在.rs文件中编写代码,可以使用uib-dropdown auto-close="disabled" 编译该文件。 Cargo将管理外部依赖项并调用extern crateextern crate行告诉编译器查找此命名空间,因此它是明确的。

  

编辑注释 - 如果您使用的是Rust 2018版本,则在许多情况下不需要rustc

rustc

extern crate ...有两个用途:

  • 与花括号一起使用时,它声明了一个模块(命名空间)。
  • 当只使用名称时,它将在本地文件系统中查找模块。

模块可以是:

  • 扩展名为.rs的文件
  • 包含一个名为mod.rs
  • 的文件的文件夹

extern crate

mod导入命名空间。我们需要在使用之前宣布我们将要使用的内容。 use子句非常严格,如果我们说明mod use中没有其他模块可用,use。星号(use module1::moduleA;)可用于使用模块中的所有内容:module1。也可以使用集合:moduleA

一个例子:

*

mod.rs包含:

use module1::*;

main.rs包含:

use module1::{moduleA, moduleB};

符号只能在模块中使用。如果您想跨越此障碍(即使是在本地声明的模块上),我们需要使用关键字| main.rs |- module1 |- mod.rs |- moduleA.rs |- moduleB.rs 将其公开。

答案 2 :(得分:0)

我参加聚会很晚,但是下面一种将代码拆分为多个文件而不影响范围的方法是

想象一下这样的文件夹结构,用于书籍处理库:

src/
  lib.rs
  author.rs
  book.rs

您可以这样做:

// lib.rs
// --------------
mod author;
use author::*;
mod book;
use book::*;

// author.rs
// --------------
struct Author {
    name: String,
    birth_year: i32,
}

// book.rs
// --------------
use super::*;

struct Book {
   title: String,
   author: Author,  // Author is in scope
   isbn: i64,
}

此结构模拟Go模块(文件夹中的所有内容似乎都在同一范围内)。

另一种方法是(更多的python风格):

// lib.rs
// --------------
mod author;
mod book;

// book.rs
// --------------
// either so, to import everything
use super::*;
// or so, one line per peer-module
use super::author;

struct Book {
   title: String,
   author: author::Author,  // Author is in scope
   isbn: i64,
}