Part 20: Utilities

Four small standard packages that almost every real program reaches for sooner or later: Time for spans, clocks and calendar dates, Random and Entropy for numbers you cannot predict — or deliberately can — Hash for fingerprints, and Uuid for names nobody else will pick. Each lesson is short and stands on its own, and together they show how the standard library speaks about failure: a step that may not fit returns an optional, and a request the system may refuse returns a fallible.

What you will learn

  • Spans of time with Duration, where every step that could overflow says so in its type.
  • Measuring elapsed time with Instant, the clock that never goes backwards.
  • Calendar dates and the leap-year rule, and reading dates with errors that point at the faulty byte.
  • Moments in time: a DateTime with a UtcOffset, RFC 3339 text, and comparing moments through Timestamp.
  • Seeded generators that replay the same numbers, and distributions that shape them into dice, shuffles and bell curves.
  • Unpredictable bytes from the operating system, and why a failure there must never be papered over.
  • Fast, non-cryptographic hashes and checksums, all at once or piece by piece.
  • Creating, printing and strictly parsing UUIDs.

The part at a glance

flowchart LR
    u(["Utilities"]) --> t["Time"]
    u --> r["Randomness"]
    u --> id["Fingerprints and names"]
    t --> span["How long?<br/>Duration, Stopwatch"]
    t --> when["When?<br/>Date, Date and time"]
    r --> rep["Repeatable<br/>Random, Distribution"]
    r --> unp["Unguessable<br/>Entropy"]
    id --> h["From data<br/>Hash"]
    id --> uu["From nothing<br/>UUID"]
    unp -. "seeds" .-> rep
    unp -. "random bits" .-> uu

Lessons

Lessons

LessonWhat you will learn
20.1Durationlengths of time, and arithmetic on them
20.2Stopwatchmeasure how long something takes
20.3Datecalendar dates, parsing them, and leap years
20.4Date and timea date with a time and an offset, in RFC 3339
20.5Randoma reproducible random number generator
20.6Distributiondraw from a range, shuffle, and sample a normal distribution
20.7Entropyunpredictable bytes from the operating system
20.8Hashfast non-cryptographic hashes and checksums
20.9UUIDcreate, print and parse UUIDs

Before you start

This part leans on the native outcomes: Part 8: Optionals for every Duration? and Date?, and Part 9: Errors for parsing and for requests the system may refuse. The randomness lessons pass generators as mutable references and call generic functions, and Entropy and Hash hand bytes over through pointers from Part 15: Memory. Each lesson's package is in the Examples repository's Utilities/ folder:

cd Examples/Utilities/Duration
rux run

A few lessons print something different on every run — a random key, a die roll, a fresh UUID. Their pages say which lines to expect to change.

After this part

Part 21: Data formats reads and writes JSON and TOML, the formats programs use to exchange and configure data.

Before moving on, try the checkpoint projects. Guess is a number-guessing game whose secret comes from a generator seeded with entropy; Age works out an age in years, months and days from two dates; Password draws a password straight from the system's entropy without favouring any character; and Launch counts down to a rocket launch with SleepFor.

For the language rules this part relies on, see Structs, Enums and Pointers in the Rux Reference.