我很难用多个嵌套文件夹构建一个Rust二进制项目.这里的目的是在一个项目中实践"Rust By Example"中列出的所有示例,并使用cargo run查看所有输出.我try 了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;

folder structure

.
|-- 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

main.rs

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();
*/
}

src/book/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;

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)));
}

推荐答案

mod声明中不能使用::.

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

pub mod ch01;

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

use self::book::ch01::formatting_example;
mod book;

Rust相关问答推荐

按下按钮时如何在服务器中创建文件并在本地下载?

将JSON密钥转换为Polars DataFrame

如何在Tauri中将变量从后端传递到前端

如何将像烫手山芋一样不透明的值从一个Enum构造函数移动到下一个构造函数?

为什么我们需要std::thread::scope,如果我们可以使用thread.join()在函数的生命周期内删除引用?

如何将映射反序列化为具有与键匹配的字段的定制 struct 的向量?

如何获取光标下的像素 colored颜色 ?

随机函数不返回随机值

从字节数组转换为字节元组和字节数组时,为什么 Transmute 会对字节重新排序?

在 Rust 中查找向量中 dyn struct 的索引

当没有实际结果时,如何在 Rust 中强制执行错误处理?

Rust编译器通过哪些规则来确保锁被释放?

Rust 程序中的内存泄漏

实现泛型的 Trait 方法中的文字

如果不满足条件,如何在 Rust 中引发错误

特征中定义的类型与一般定义的类型之间的区别

Rust - 在线程之间不安全地共享没有互斥量的可变数据

当特征函数依赖于为 Self 实现的通用标记特征时实现通用包装器

为什么我不能将元素写入 Rust 数组中移动的位置,但我可以在元组中完成

在同一向量 Rust 中用另一个字符串扩展一个字符串