I am having difficulty in building a Rust binary project with multiple nested folders. The intent here is to practice all the examples listed in 'Rust By Example' in one single project and use cargo run
to see all the output. I have tried various combinations of the use
and mod
keywords, but I'm unable to wrap my head around them.
This is the error I get:
$ 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)));
}
You cannot use ::
in a mod
declaration.
You need a file src/book/mod.rs
, containing:
pub mod ch01;
And in your main.rs
file, use:
use self::book::ch01::formatting_example;
mod book;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With