[英]How do I do a basic import/include of a function from one module to another in Rust 2015?
我找不到如何将函数从一个文件(模块)包含(或导入、注入或其他词)到另一个文件(模块)。
我开始一个新项目
$ cd ~/projects
$ cargo new proj --bin
$ cd proj
$ tree
.
|
-- Cargo.toml
-- src
|
-- main.rs
我修改main.rs
并使用以下代码创建一个新文件a.rs
(在src
目录中):
主文件
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
我已经阅读 了指南,但仍然无法弄清楚如何进行导入。
在 Rust 中,有一些关键字来处理模块:
extern crate
extern crate
填补了 Cargo 和 Rust 之间的空白。 我们在一个 .rs 文件中编写代码,这个文件可以用rustc
编译。 Cargo 将管理外部依赖项并调用rustc
。 extern crate ...
行告诉编译器寻找这个命名空间,所以它是明确的。
编者注- 如果您使用的是 Rust 2018 版本,则在许多情况下不需要
extern crate
。
mod
mod
有两个用途:
模块可以是:
use
use
导入一个命名空间。 我们需要在使用之前宣布我们将使用什么。 use 子句非常严格,如果我们声明use module1::moduleA;
没有其他模块module1
将可用,但moduleA
。 星号 ( *
) 可用于使用模块中的所有内容: use module1::*;
. 也可以使用集合: use module1::{moduleA, moduleB};
一个例子:
| main.rs
|- module1
|- mod.rs
|- moduleA.rs
|- moduleB.rs
mod.rs 包含:
pub mod moduleA; // declare a child module
pub mod moduleB; // declare a child module
main.rs 包含:
/// ======
// use what Cargo downloaded
extern crate that_one_thing_i_need;
/// ======
mod module1; // declare a child module
// some local stuff I want to scope
mod local {
pub fn my_function() {}
}
// ======
// make the symbols locally available:
use module1::moduleA::*;
use module1::moduleB::{functionX, moduleY, typeZ};
// we still need to announce what stuff from the external crate
// we want to use:
// We can do local aliases that will be valid in this one file.
use that_one_thing_i_need::fancy_stuff as fs;
/// ======
fn main() {
// we can use anything here from the namespaces we are using:
// moduleA
// functionX
// moduleY
// typeZ
// fs
// We can access stuff by navigating from the outermost visible
// module name
local::my_function();
}
符号只能在模块内使用。 如果你想跨越这个障碍(即使是在本地声明的模块上),我们需要使用关键字pub
将它们公开。
我参加聚会很晚,但是将代码拆分为多个文件而不会过多影响范围的一种方法如下。
想象一下这样的文件夹结构,对于书籍处理库:
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,
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.