diff options
| author | Jasper Ras <jras@hostnet.nl> | 2025-08-21 22:09:29 +0200 |
|---|---|---|
| committer | Jasper Ras <jras@hostnet.nl> | 2025-08-21 22:09:29 +0200 |
| commit | 27514d58573ce1f844af4ea60afa72c7f58f1cd7 (patch) | |
| tree | 4b16c31f73cb72435d03d5e105261143f8ca135f /Cargo.md | |
| parent | 22675cd8dc75d8b8d4b0f818f5b093efbc364802 (diff) | |
Diffstat (limited to 'Cargo.md')
| -rw-r--r-- | Cargo.md | 35 |
1 files changed, 34 insertions, 1 deletions
@@ -1,4 +1,37 @@ -[[Rust]] +Module -> Crate -> Package + +Binary Crate +Library Crate (tip: should contain the module tree, binary crate should be a client to the lib) + +Package: n binary crates, 1 library crate +`src/main.rs` package exports binary crate of package name +`src/bin/*.rs` multiple binary crates +`src/lib.rs` package exports library crate of package name + +--- + +Absolute import path start with `crate::` etc. +Relative import path start with `self::` , `super::`, or an identifier in the current mod. +`super` starts rel path from the parent module. +child scope can access everything in parent scope even privates. + +`use` to create a shortcut to crates **only** in the **current** scope. + +It's idiomatic when calling a function to import its parent module so it's clear that the function call is not to a local function. For importing structs and enums and such it's idiomatic to import them all the way (unless its name collides ofc). + +`use .. as name` is also possible. + +`pub use ..` to re-export it so external callers can also reference it shortly, exposing a different structure to callers than the internal package actually is. + +Nesting `use` stmts: `use std::{cmp::Ordering, io};` +or `use std::io::{self, Write};` instead of `std::io;` and `std::io::Write;`. + +Glob: `use std::collections::*;` + +`pub mod blaat;` -> `src/blaat.rs` -> `pub mod henk;` -> `src/blaat/henk.rs`. +old-style: `src/blaat/mod.rs` -> `src/blaat/henk/mod.rs` (many mod.rs files) + +--- Standard stuff: - cargo build |
