# Microsoft Rust Training Team - Rust Patterns
## Metadata
**Source**:: #from/zotero
**Zettel**:: #zettel/fleeting
**Status**:: #x
**Authors**:: [[Microsoft Rust Training Team]]
**Full Title**:: Rust Patterns
**Category**:: #book
**Date**:: [[2026]]
**Created**:: [[2026-04-14]]
**Document Tags**:: #
**Zotero App Link**:: [Open in Zotero](zotero://select/library/items/7Y5NQVWJ)
**Zotero Web Link**:: [zotero.org](https://www.zotero.org/ianyi/items/7Y5NQVWJ)
## Annotations 2026-04-14
- const fn marks a function as evaluable at compile time —Rust’s equivalent of C++ constexpr. [📄p. 21](zotero://open-pdf/library/items/LVW3KXXA?page=31&annotation=4ABYNRUG) ^4ABYNRUG
\`const fn\` can be defined in \`impl\` block.
- What you CANNOT do (yet): - Heap allocation (Box, Vec, String) - Trait method calls (only inherent methods) - Floating-point in some contexts (stabilized for basic ops) - I/O or side effects [📄p. 23](zotero://open-pdf/library/items/LVW3KXXA?page=33&annotation=KJHJJ9BN) ^KJHJJ9BN
- Intuition: If it makes sense to ask “what is the Item of this iterator?”, use associated type. If it makes sense to ask “can this convert to f64? to String? to bool?”, use a generic parameter. [📄p. 31](zotero://open-pdf/library/items/LVW3KXXA?page=41&annotation=CZUDNSYL) ^CZUDNSYL
#intuition
- Since Rust 1.65, associated types can have generic parameters of their own. This enables lending iterators — iterators that return references tied to the iterator rather than to the underlying collection [📄p. 32](zotero://open-pdf/library/items/LVW3KXXA?page=42&annotation=BBN6TRQ4) ^BBN6TRQ4
\`type X where …\`
- Caution: Blanket impls are powerful but irreversible — you can’t add a more specific impl for a type that’s already covered by a blanket impl (orphan rules + coherence). [📄p. 36](zotero://open-pdf/library/items/LVW3KXXA?page=46&annotation=45HSH4CR) ^45HSH4CR
- Rule of thumb: If you plan to use dyn Trait, keep methods simple — no generics, no Self in return types, no Sized bounds. When in doubt, try let _: `Box<dyn YourTrait>`; and let the compiler tell you. [📄p. 39](zotero://open-pdf/library/items/LVW3KXXA?page=49&annotation=ZFE4HRWA) ^ZFE4HRWA
- The Rust API Guidelines (C-DEREF) state: “only smart pointers should implement Deref.” Treat this as a strong default; deviate only with clear justification. [📄p. 102](zotero://open-pdf/library/items/LVW3KXXA?page=112&annotation=AT4E9MKA) ^AT4E9MKA
- `Borrow<T> vs AsRef<T>`: Both provide &T, but `Borrow<T>` additionally guarantees that Eq, Ord, and Hash are consistent between the original and borrowed form. This is why `HashMap<String, V>::get()` accepts `&Q where String: Borrow<Q>` — not AsRef. Use Borrow when the borrowed form is used as a lookup key; use AsRef for general “give me a reference” parameters. [📄p. 357](zotero://open-pdf/library/items/LVW3KXXA?page=367&annotation=7TQ2NM9A) ^7TQ2NM9A
- “Parse, don’t validate” is a principle that says: don’t check data and then pass around the raw unchecked form — instead, parse it into a type that can only exist if the data is valid. [📄p. 361](zotero://open-pdf/library/items/LVW3KXXA?page=371&annotation=JHDJFHB4) ^JHDJFHB4
- Use dep: syntax (Rust 1.60+) for optional dependencies to avoid creating implicit features [📄p. 370](zotero://open-pdf/library/items/LVW3KXXA?page=380&annotation=Y469A6EJ) ^Y469A6EJ
- # Cargo aliases — custom shortcut commands [alias] [📄p. 372](zotero://open-pdf/library/items/LVW3KXXA?page=382&annotation=C4DCQTCR) ^C4DCQTCR
- # Environment variables for build scripts [env] [📄p. 372](zotero://open-pdf/library/items/LVW3KXXA?page=382&annotation=NS5NBA5R) ^NS5NBA5R
- cargo deny and cargo audit: Supply-Chain Security [📄p. 376](zotero://open-pdf/library/items/LVW3KXXA?page=386&annotation=SEZVMA7L) ^SEZVMA7L