当存在嵌套的源目录时,“use”和“mod”如何工作?

roh*_*hta 8 rust

我在构建具有多个嵌套文件夹的 Rust 二进制项目时遇到困难。这里的目的是在一个项目中练习“Rust By Example”中列出的所有示例,并用于cargo run查看所有输出。我尝试了usemod关键字的各种组合,但我无法理解它们。

这是我得到的错误:

$ cargo run
   Compiling rustbyexample v0.1.0 (file:///xxx/rustProjects/rustbyexample)
src/main.rs:6:9: 6:11 error: expected one of `;` or `{`, found `::`
src/main.rs:6 mod book::ch01;
Run Code Online (Sandbox Code Playgroud)

文件夹结构

.
|-- Cargo.lock
|-- Cargo.toml
|-- src
|   |-- book
|   |   |-- ch01
|   |   |   |-- customDisplay.rs
|   |   |   |-- display_list.rs
|   |   |   |-- formatting.rs
|   |   |   |-- mod.rs
|   |   |   `-- tuple_example.rs
|   |   `-- ch02
|   |       `-- arrayandslices.rs
|   |-- coursera
|   |   `-- week1
|   |       `-- caesarcipher.rs
|   |-- lib.rs_neededforalibrary
|   `-- main.rs
`-- target
`-- debug
    |-- build
    |-- deps
    |-- examples
    |-- native
    `-- rustbyexample.d
Run Code Online (Sandbox Code Playgroud)

主文件

use self::book::ch01;
//use book::ch01::customDisplay::display_example as display_example;
//use book::ch01::display_list::print_list_example as print_list;
//use book::ch01::tuple_example::tuple_example as tuple_example;

mod book::ch01;
//mod formatting;
//mod customDisplay;
//mod display_list;
//mod tuple_example;

fn main() {
println!("Main Rust Program to call others.");

println!("********** Formatting Example   ****************");
formatting_example();
/*
println!("********* Implementing Display Example *************");
display_example();

println!("**** Implement Display to Print Contents of List *****");
print_list_example();

println!("**** Implement Tuple Related Example ****");
tuple_example();
*/
}
Run Code Online (Sandbox Code Playgroud)

源代码/书/ch01/mod.rs

pub use self::formatting::formatting_example;
   //use book::ch01::customDisplay::display_example as display_example;
   //use book::ch01::display_list::print_list_example as print_list;
   //use book::ch01::tuple_example::tuple_example as tuple_example;

   pub mod formatting;
   //mod customDisplay;
   //mod display_list;
   //mod tuple_example;
Run Code Online (Sandbox Code Playgroud)

src/book/ch01/formatting.rs

#[derive(Debug)]
struct Structure(i32);

#[derive(Debug)]
struct Deep(Structure);

pub fn formatting_example() {
    println!("{:?} months in a year.", 12);

    println!("{1:?} {0:?} is the {actor:?} name.", "Slater", "Christian", actor="actor's");

    // `Structure` is printable!
    println!("Now {:?} will print!", Structure(3));

    // The problem with `derive` is there is no control over how
    // the results look. What if I want this to just show a `7`?
    println!("Now {:?} will print!", Deep(Structure(7)));
}
Run Code Online (Sandbox Code Playgroud)

ant*_*oyo 9

您不能::mod声明中使用。

您需要一个文件src/book/mod.rs,其中包含:

pub mod ch01;
Run Code Online (Sandbox Code Playgroud)

在您的main.rs文件中,使用:

use self::book::ch01::formatting_example;
mod book;
Run Code Online (Sandbox Code Playgroud)

  • 谢谢安托约。Shepmaster 的评论给我指出了正确的章节。希望在前面的章节中讨论该主题以避免这种情况。 (2认同)