我在使用多个嵌套文件夹构建Rust二进制项目时遇到了困难。这里的目的是练习' Rust By Example'中所列的所有示例。在一个项目中,使用cargo run
查看所有输出。我尝试了use
和mod
个关键字的各种组合,但我无法绕过它们。
这是我得到的错误:
$ 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;
文件夹结构
.
|-- 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 /书/ 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 /书/ 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)));
}
答案 0 :(得分:3)
您无法在::
声明中使用mod
。
您需要一个文件src/book/mod.rs
,其中包含:
pub mod ch01;
在main.rs
文件中,使用:
use self::book::ch01::formatting_example;
mod book;