Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Appendix A: Keywords

Kāra keeps its hard keywords few, so that common words stay usable as names. This appendix follows design.md § Keywords, which is the authority.

Hard keywords

A hard keyword starts an item, a statement or an expression, or is an operator spelled as a word. It can't be used as an identifier, except as a raw identifier (r#match) or as a member name (below).

KeywordPurpose
fnDeclare a function
structDeclare a struct
enumDeclare an enum
traitDeclare a trait
implImplement a trait or add methods to a type
typeDeclare a type alias
constDeclare a compile-time constant
importImport a module or a name from one
externDeclare foreign functions or types
unsafeMark a block or function as bypassing safety checks
pubPublic: visible outside the package
privatePrivate: visible only within the current directory
letDeclare a local binding
mutMark a binding as mutable, or a parameter or argument as written through
if, elseConditional branch
matchPattern-matching switch
whileCondition-driven loop
for, inIterator-driven loop
loopInfinite loop
returnEarly return from a function
breakExit from a loop
continueSkip to the next loop iteration
deferRun a block when the enclosing scope exits, on every exit (normal, return, break or ?)
errdeferRun a block when the enclosing scope exits with an error (? or returning Err / None)
refBorrow a value (read-only reference)
asType cast
and, or, notLogical operators
true, falseBoolean literals
selfThe receiver value within a method
SelfThe type of the current impl block or trait
dynDynamic dispatch through a trait object: dyn Trait

(Default visibility, with no keyword, is project-internal: visible to every file in the project.)

use and mod are not keywords. Modules come from the directory tree, and imports use import.

Contextual keywords

A contextual keyword is a keyword only in one position, and an ordinary identifier everywhere else. A field called reads or a variable called test is fine.

WordA keyword only when it
parcomes directly before {, for or (
unionstarts an item
distinctcomes directly before type
markercomes directly before trait
shared, synccome directly before struct or enum
weak, frozenprefix a type or a receiver
freezeprefixes an expression
escapingprefixes a function type or a dyn type
owncomes directly before a parameter's type, before self, or before an argument type in a function type (Fn(own T) -> R); it marks an owned parameter
hostcomes directly before fn at item position
testcomes directly before a string literal at module scope
wherefollows a signature or a type head
withstarts an effect clause
effect, resourcestart an effect resource declaration
reads, writes, sends, receives, allocates, panics, blocksappear inside an effect clause or an effect attribute such as #[no_effect(...)]

So these are legal:

struct IoStats { reads: i64, writes: i64, blocks: i64 }
fn union(mut ref self, a: i64, b: i64) { ... }
fn create_server(host: String, port: u16) { ... }

Words that belong to deferred features (lock, seq, layout, group, stable, alias, independent, providers, requires, ensures, invariant, suspends) are not keywords in v1. They come back as contextual keywords with their features.

Keywords as member names

A member name may be any keyword without r# in two positions where no keyword can start anything: directly after . (opt.or(fallback), handle.await), and as the name of a method or associated function declared in an impl or trait. A free function, a local, a field or a type with a keyword's name needs r#.

Reserved for future use

These words are reserved now, so a later feature can use them without breaking code. Using one as an identifier is error[E_RESERVED_KEYWORD], and the fix is r#name.

KeywordWhy reserved
async, awaitKāra marks suspension with the suspends effect rather than function colouring
becomeGuaranteed tail calls
boxAn owned heap-pointer primitive
comptimeCompile-time evaluation
doBlock-expression sugar
final, override, virtualSubclass-style markers, common in code ported from OOP languages
gen, yieldGenerators
asm, global_asmInline and module-level assembly
moveRust's capture-by-value keyword; Kāra infers capture modes
privToo easily confused with private
pureIts one use in v1: before fn in an extern block, it declares that the foreign function has no effects
trytry { ... } blocks
typeofType-of queries

Primitive type names

i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, f32, f64, bool, char and ! (the never type) are always in scope and need no import.