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