From 27514d58573ce1f844af4ea60afa72c7f58f1cd7 Mon Sep 17 00:00:00 2001 From: Jasper Ras Date: Thu, 21 Aug 2025 22:09:29 +0200 Subject: vault backup: 2025-08-21 22:09:29 --- Cargo.md | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'Cargo.md') diff --git a/Cargo.md b/Cargo.md index baa57ed..009aff9 100644 --- a/Cargo.md +++ b/Cargo.md @@ -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 -- cgit v1.2.3