Atoms
Atoms are named values such as :draft and :published. Literal atom types
can restrict a parameter to a fixed set of choices. Booleans are the atoms
true and false; conditions combine them with and, or, and not.
Named values
Section titled “Named values”:draft is an atom; "draft" is a string. Use atoms for known labels and
strings for text supplied by a user. The type Atom accepts any atom, while
:draft | :published accepts exactly those two values:
pub fn publish(status: :draft | :published, allowed: Boolean) -> :draft | :published { if allowed { :published } else { status }}publish(:draft, true) returns :published. Passing :archived is a type
error. Atom literals are values and can also name their own singleton types.
Booleans and conditions
Section titled “Booleans and conditions”Boolean contains true and false, two atoms with special literal syntax.
An if selects a value and must include an else branch.
One rule, two uses
Section titled “One rule, two uses”An author or editor may publish an unlocked draft. A lock blocks both roles. Keep that rule in one function so the button label and the action can use the same decision:
mod atoms
pub fn can_publish(is_author: Boolean, is_editor: Boolean, is_locked: Boolean) -> Boolean { (is_author or is_editor) and not is_locked}
pub fn publish_label(is_author: Boolean, is_editor: Boolean, is_locked: Boolean) -> String { if can_publish(is_author, is_editor, is_locked) { "Publish" } else { "Read only" }}or admits either role; and not is_locked applies to the combined result.
The parentheses make that grouping explicit. and and or short-circuit:
they evaluate the right operand only when needed.
This is a decision function, not authentication. Its caller supplies the roles and lock state from a trusted source and must enforce the result when publishing.
Conditions must be Boolean
Section titled “Conditions must be Boolean”An if is an expression, so both branches must return compatible types.
Compare a count with zero to produce a Boolean condition:
pub fn count_label(count: Integer) -> String { if count > 0 { "some" } else { "none" }}count > 0 returns true or false; the selected branch returns a string.