Tutorial by Examples

Files: - example.rs (root of our modules tree, generally named lib.rs or main.rs when using Cargo) - first.rs - second/ - mod.rs - sub.rs Modules: - example -> example - first -> example::first - second -> example::second - sub -> exampl...
Rust's #[path] attribute can be used to specify the path to search for a particular module if it is not in the standard location. This is typically discouraged, however, because it makes the module hierarchy fragile and makes it easy to break the build by moving a file in a completely different dire...
The double-colon syntax of names in the use statement looks similar to names used elsewhere in the code, but meaning of these paths is different. Names in the use statement by default are interpreted as absolute, starting at the crate root. Names elsewhere in the code are relative to the current mo...
Sometimes, it can be useful to import functions and structs relatively without having to use something with its absolute path in your project. To achieve this, you can use the module super, like so: fn x() -> u8 { 5 } mod example { use super::x; fn foo() { println!(...
Directory structure: yourproject/ Cargo.lock Cargo.toml src/ main.rs writer.rs main.rs // This is import from writer.rs mod writer; fn main() { // Call of imported write() function. writer::write() // BAD writer::open_file() } w...
Let's see how we can organize the code, when the codebase is getting larger. 01. Functions fn main() { greet(); } fn greet() { println!("Hello, world!"); } 02. Modules - In the same file fn main() { greet::hello(); } mod greet { // By default, everything inside a...

Page 1 of 1