A Tour of hd
Each page makes one claim about hd, explains in a few sentences why it matters, and gives you a program to run and change right in the page. Most pages also name a one-line edit that turns the program into a compile error, so you see the error the claim is about.
Contents
- Hello, hdA program starts at
main, andmainsays what it may touch - Values and mutA struct is a value, so a function changes yours only if you hand it over with
&mut - FunctionsA public signature is a contract, checked on both sides
- Strings and numbersA number never changes type on its own, so every conversion is in the code
- Enums and matchA
matchcovers every case, so a new case can't be forgotten - Option, not nullThere is no null: a value that may be missing says so in its type
- Errors and ?Errors are values in the return type, and
?passes them up - Control flow as values
if,match, and loops produce values, so a name is set once on every path - PipesA pipe reads in the order things happen, and
_shows where the value goes - Data and copy-with-changesA record says which fields are required, and
...copies one with changes - Sharing with &mutTwo holders see one object only when you write
&mut - Element references
for p in &mut playerschanges the elements in place, and the list holds still while you do - Lists are valuesA copy of a list never changes when the original does, and copying is cheap
- Generics and closuresOne generic function serves every type its bound allows, and the bound is checked
- Collections and iteratorsA list or map is typed by what it holds, and a lookup that may miss says so
- Traits and methodsA trait is a checked contract: leave out a method and the type doesn't compile
- Requirements and providersA signature lists what a function touches, so a test can hand it a fake
- TestsTests sit next to the code they check, with nothing to install
- Modules and pubA file shares only what it marks
pub, so its helpers stay its own business - Folders and packagesA folder's
mod.hdlists what the folder offers, and another package is one more root - Error enums and context
.contextturns a low-level failure into a report that says what you were doing - Suspension and all!A function that can wait says so with
!, and so does every call to it - Least authority in main
main's row is everything the program may touch - DeriveOne
@deriveline gives a type random test inputs, so a property test finds the case you missed - Where nextYou have seen the core of hd; the playground, the guide, and the spec go further
Want the syntax on one page? Read Learn hd in 10 Minutes.