27Roadmap
What comes after version 1.0, and what 1.0 knowingly leaves open. Version 1.0 freezes the language and the standard library's shape: a breaking change went in before the freeze, and what follows is additive, or an internal rewrite that changes no program's meaning. Nothing here has a date. The order within each list is not a schedule.
Known limitations in 1.0
Four defects are known and left open for 1.0. Each has a workaround, and each is a program that is refused or a program that compiles when it should not, never a miscompile of a program 1.0 accepts.
A string view can outlive the text it points into. A Str taken from an element of a temporary Array<String> and assigned to a variable that lives longer than the array compiles without a word, and reading it afterwards reads freed memory. Keeping a view only where it cannot outlive its owner is phase 3 of the memory-safety rollout, which 1.0 does not enforce. Until it is, bind the collection to a local that lives as long as the view, or copy the text into a String you own:
import std::collections::str::{ Str };
import std::collections::string::{ String };
function words(text: Str) -> Array<String> {
mut result: Array<String> = Array::<String>::new();
result.push(String::from_str(text));
return result;
}
function main() -> int {
const all: Array<String> = words(Str::new("kept")); // lives as long as `kept` does
mut kept: Str = Str::new("");
for (word in all.iterate_references()) {
kept = word.as_str();
}
return if (kept.length() == 4) { 0 } else { 1 };
}
T::from on a type parameter needs a bound. A path call on a type parameter finds methods only through the body's bounds, so T::from(value) in a body whose T has no bound is refused with E0358, even though implement<T> trait From<T> for T holds for every type. Write the bound:
import std::core::convert::{ From };
function through_from<T>(value: T) -> T where T: From<T> {
return T::from(value);
}
function main() -> int {
return if (through_from(7) == 7) { 0 } else { 1 };
}
A for loop in a generic body cannot loop over a parameter bounded only by Iterable. Such a body is refused where it is written (E0306), whether or not anything calls it. Bound the parameter by Iterator and pass it the collection's iterate(), or loop where the collection's type is known:
import std::core::iter::{ Iterator };
function count_all<I>(items: I) -> i64 where I: Iterator<i32> {
mut n: i64 = 0;
for (item in items) { n = n + 1; }
return n;
}
function main() -> int {
mut numbers: Array<i32> = Array::new();
numbers.push(1);
numbers.push(2);
return if (count_all(numbers.iterate()) == 2) { 0 } else { 1 };
}
A Windows program links standard-library code it does not use. The empty program is about 1.7 MB on Windows, because an always-linked object holds a generic instance whose methods reach the hash map's keyed hasher and, through it, the secure random source. Nothing runs that the program does not call; it costs size only.
Memory safety
Version 1.0 enforces phase 1 of the memory-safety rollout: every local is initialised before it is read, no reference is carried out of its function, generic bodies are checked once against their own parameters, values are released once, and the run-time checks on indexing, division and shifts. The later phases are designed and not built. Each arrives in a 1.x release as a warning, so programs can move to it, and becomes an error at 2.0:
| phase | rules | 1.x warning | 2.0 error |
|---|---|---|---|
| 2 | the operations that need unsafe, refused outside it | W0019 | E0462 |
| 3 | views: a reference kept only where it cannot outlive its owner, and the collections handing out &T instead of T* | W0020 | E0461 |
| 4 | loans: a place not moved, released or assigned while a reference to it is used, and shared-or-writable at every loan | W0021, W0022 | E0450, E0451 |
Phases 5 (threads, with a thread-marker type) and 6 (the collections' change counters) come later still.
The language
- Loops over references and generic collections. An iterator that yields
&T, so a borrowing loop binds a reference instead of a pointer, and with it a loop over&[T]and the other references to an iterable; and a loop in a generic body over a parameter bounded byIterable. T::fromon an unbounded type parameter. Member lookup on a type parameter answered by an implementation that holds for every type, so the bound above is no longer needed.- Tuples.
Eq,Clone,DebugandDisplayfor tuples structurally, as they haveCopy,SendandSync, and method calls on a tuple.Pairstays the convention for a public two-value result. - Text. A string literal whose type nothing declares becomes a
Str, so"a,b".contains(",")findsStr's methods directly; and==with astringon the left of aStr. - Trait objects. Owning boxes, which take their drop, size and alignment from the table, and upcasting a subtrait object to one of its supertraits. Borrowed trait objects are in 1.0.
- Associated constants of a trait used as values.
- Paths through generic type aliases:
type Stack<T> = Array<T>; Stack::<i32>::new(). - Capturing closures in function-typed slots: a function value that carries its captures, so a capturing closure can be stored in a variable, field or element of function type.
- Compound assignment through traits of its own, so
+=on aStringappends in place. - A checked conversion from an integer to an enum, answering
Optionfor a value no variant holds. - One literal form in a variant's payload. An integer or float written directly as a payload element,
Some(-7)orCelsius(36.6), is its own kind of element today, tested apart from the literal pattern a tuple element or a nested pattern holds,Some((-1, 'q')). Both become the nested literal pattern, so one test and one type check serve every literal wherever it is written. - Compile-time execution, and with it a context allocator scope for the
newexpressions inside a block.
Packages and the toolchain
- Two major versions of one library in a build: each package takes the major version it asks for, with versioned symbols and a rename key in
cryoconfig. In 1.0 a build holds one version of each library. - A package registry, naming a library and a version requirement without a URL.
- A lint tool built on the compiler as a library, as the language server is.
- The compiler's pipeline: the checked tree lowered through a high-level and a mid-level representation before LLVM IR.
The standard library
Left to packages: regular expressions, compression, cryptography beyond SHA-1, SHA-256 and HMAC, big integers, memory-mapped files, the time zone database, Unicode normalization and collation, glob matching, and the less common collections. After 1.0 in the library itself: a reactor for pipes on Windows (asynchronous child I/O works through the blocking pool today), and a core tier free of the C library for the runtime.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
type struct Str {}A read-only view of UTF-8 text owned elsewhere: a string literal, a String, a file's bytes. Copying a Str copies the view, not the text, and it is valid only while what it views is. Str::new("text") views a literal; String.as_str() views a String.
type struct String<A = GlobalAllocator> {}Owned, growable UTF-8 text. It dereferences to Str, the read-only view most text functions take, so every Str method reads a String too (s.contains(...), s.split(...)); as_str hands the view out.
function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function words(text: Str) -> Array<String>(parameter) text: Strtype struct Array<T, A = GlobalAllocator> where A: Allocator {}A growable sequence of Ts stored contiguously, owning its elements; A is the allocator its storage comes from. Indexing out of range panics; get answers None instead.
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut result: Array<String>static Array::new() -> Array<T, GlobalAllocator>Empty array backed by GlobalAllocator.
mut result: Array<String>Array::push(mut &this, value: T) -> voidAdd value after the last element, growing the storage when it is full. Panics on allocation failure; try_push returns the error instead.
static String::from_str(source: Str) -> StringConstruct from a Str by copying its bytes. Panics on allocation failure.
(parameter) text: Strreturn value;Leaves the current function with the given value. A function whose return type is not void must return one; a bare return; is for void. In an async function it completes the future with the value.
function main() -> intconst name: Type = value;An immutable binding: it cannot be reassigned after initialisation. The annotation may be omitted when an initialiser is present - the binding takes the initialiser's concrete type, inferred locally. Mutability is opt-in through mut.
const all: Array<String>function words(text: Str) -> Array<String>static Str::new(text: string) -> StrConstruct from a NUL-terminated C-style string literal. The caller certifies that text is valid UTF-8 and outlives this view. The NUL terminator is not included in the view.
mut kept: Strfor (mut i: i32 = 0; i < n; i++) { ... }
for (item in iterable) { ... }
for ((i, item) in items.iterate().enumerate()) { ... }Two forms. The C-style for (init; condition; step) scopes its loop variable to the body. for (x in expr) iterates an Iterator, a range such as 0..n, or an Iterable, which it consumes: an Array, a Slice of Copy elements, or a fixed-size array. A loop that only borrows writes its borrowing iterator, xs.iter() or xs.iterate_references(). The binding may be a tuple pattern that takes each item apart, nested and with _ elements: enumerate() yields (index, element) with an i64 index, and zip yields (a, b). A loop variable the body never reads is warned about (W0001); write _.
word: String*for (item in iterable) { ... }Separates the loop variable from the sequence in a for loop. The sequence is evaluated once; the loop lowers to loop { match (it.next()) { Some(x) => .., None => break } }.
const all: Array<String>Array::iterate_references(&this) -> implement Iterator<T*>Iterate by shared borrow, yielding T* for each element in order. Unlike iter, this is sound for every T (Copy or owning) because it hands out borrows, never copies. The pointers are invalidated by any reallocating method (push, reserve, insert, shrink_to_fit).
mut kept: Strword: String*String::as_str(&this) -> StrBorrow as a Str. Invalidated by the next mutating call.
if (condition) { ... } else if (condition) { ... } else { ... }Branches on a parenthesised boolean condition. Bodies are always braced; there is no single-statement form. Used as an expression, both arms are required and must produce the same type: const sign = if (n < 0) { -1 } else { 1 };.
Str::length(&this) -> i64Byte length. For a UTF-8 string, length() >= char_count.
if (condition) { ... } else { ... }The branch taken when every preceding if condition is false; else if chains another condition. In expression form the else arm is mandatory, so the expression always has a value.
type trait From<T> {
static from(value: T) -> This;
}A conversion that always succeeds, from a T into this type.
function through_from<T>(value: T) -> T
where T: From<T>type parameter Twhere T: From
(parameter) value: Twhere T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
(parameter) value: Tfunction through_from<T>(value: T) -> T
where T: From<T>type trait Iterator {
type Item;
next(mut &this) -> Option<This::Item>;
count(mut &this) -> i64;
fold<Acc, F>(mut &this, initial: Acc, f: F) -> Acc
where F: (Acc, This::Item) -> Acc;
for_each<F>(mut &this, f: F) -> void
where F: (This::Item) -> void;
take(this, n: i64) -> implement Iterator<This::Item>;
skip(this, n: i64) -> implement Iterator<This::Item>;
map<B, F>(this, f: F) -> implement Iterator<B>
where F: (This::Item) -> B;
filter<P>(this, pred: P) -> implement Iterator<This::Item>
where P: (&This::Item) -> boolean;
chain<J>(this, other: J) -> implement Iterator<This::Item>
where J: Iterator<This::Item>;
enumerate(this) -> implement Iterator<(i64, This::Item)>;
zip<J>(this, other: J) -> implement Iterator<(This::Item, J::Item)>
where J: Iterator;
collect<C>(this) -> C
where C: FromIterator<This::Item>;
sum<S>(this) -> S
where S: Sum, This: Iterator<S>;
reverse(this) -> implement Iterator<This::Item>
where This: DoubleEndedIterator;
peekable(this) -> implement Peekable<This::Item>;
flat_map<B, F>(this, f: F) -> implement Iterator<B::Item>
where F: (This::Item) -> B, B: Iterator;
flatten(this) -> implement Iterator<This::Item::Item>
where This::Item: Iterator;
take_while<P>(this, pred: P) -> implement Iterator<This::Item>
where P: (&This::Item) -> boolean;
skip_while<P>(this, pred: P) -> implement Iterator<This::Item>
where P: (&This::Item) -> boolean;
step_by(this, step: i64) -> implement Iterator<This::Item>;
filter_map<B, F>(this, f: F) -> implement Iterator<B>
where F: (This::Item) -> Option<B>;
any<P>(mut &this, pred: P) -> boolean
where P: (This::Item) -> boolean;
all<P>(mut &this, pred: P) -> boolean
where P: (This::Item) -> boolean;
find<P>(mut &this, pred: P) -> Option<This::Item>
where P: (&This::Item) -> boolean;
min(mut &this) -> Option<This::Item>
where This::Item: Ord;
max(mut &this) -> Option<This::Item>
where This::Item: Ord;
min_by<F>(mut &this, compare: F) -> Option<This::Item>
where F: (&This::Item, &This::Item) -> Ordering;
max_by<F>(mut &this, compare: F) -> Option<This::Item>
where F: (&This::Item, &This::Item) -> Ordering;
min_by_key<K, F>(mut &this, key: F) -> Option<This::Item>
where F: (&This::Item) -> K, K: Ord;
max_by_key<K, F>(mut &this, key: F) -> Option<This::Item>
where F: (&This::Item) -> K, K: Ord;
last(mut &this) -> Option<This::Item>;
nth(mut &this, n: i64) -> Option<This::Item>;
position<P>(mut &this, pred: P) -> Option<i64>
where P: (This::Item) -> boolean;
product<S>(this) -> S
where S: Product, This: Iterator<S>;
}A source of values handed out one at a time by next, until it returns None. for (x in source) drives one, and the adapter methods below build new iterators from it:
function count_all<I>(items: I) -> i64
where I: Iterator<i32>type parameter Iwhere I: Iterator
(parameter) items: Imut n: i64item: I::Item(parameter) items: Imut n: i64mut numbers: Array<i32>mut numbers: Array<i32>function count_all<I>(items: I) -> i64
where I: Iterator<i32>function value<T>() -> TA uniform value of type T: any integer width over its whole range, boolean, f32/f64 in [0.0, 1.0), or a char that is any Unicode scalar value.
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
type parameter Ttype trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencetype trait Iterable {
type Iteration;
iterate(this) -> This::Iteration;
}A value a for loop can loop over that is not itself an Iterator: the loop consumes it, iterate turns it into the iterator the loop drives, and each element is handed to the body as a value of its own.
unsafe { ... }The boundary of the checked language: inside it the unsafe operations are allowed - reading or writing through a raw pointer, delete, an integer-to-pointer as, a union field read, a call to an extern or unsafe function, inline asm - whose soundness the program keeps instead of the compiler. unsafe function and unsafe implement mark a promise the same way. Version 1.0 does not yet refuse these operations outside unsafe; a 1.x release warns, and 2.0 refuses.
type trait Eq<Rhs = This> {
equals(&this, other: &Rhs) -> boolean;
}Types that support value equality with a value of Rhs, which is the type itself unless an implementation names another: implement trait Eq for Str compares two Strs, and implement trait Eq<string> for Str a Str with a string.
type trait Clone {
clone(&this) -> This;
}A type whose values can be duplicated into independent copies. A type takes it with with [Clone] on its declaration, or by implementing clone by hand.
type trait Debug {
fmt(&this, f: mut &Formatter) -> Result<(), FormatError>;
}How a value reads to a programmer: what {x:?} writes, and {x:#?} over several lines. with [Debug] on a declaration writes it field by field.
type trait Display {
fmt(&this, f: mut &Formatter) -> Result<(), FormatError>;
to_string(&this) -> String;
}How a value reads to a person: what {x} in an f-string and println write. Implement it for a type a user sees.
type trait Copy {}Types whose values can be duplicated by a bitwise copy. Integers, floats, pointers, and pure-POD structs qualify; anything that owns resources (heap storage, file handles, refcount) does not.
std::core::markertype trait Send {}Types that are safe to transfer between threads. An owning value of a Send type may cross a thread boundary without violating memory safety.
type trait Sync {}Types that are safe to share between threads by shared reference. T: Sync if &T: Send.
type struct Pair<A, B> with [Eq, Clone] {
first: A;
second: B;
}Two values held together, first and second: a map's key with its value.
primitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
type Name = Existing;A type alias: a new, transparent name for an existing type - the two are interchangeable everywhere. Aliases may be generic: type StringResult<T> = Result<T, string>;.
type Stack<T>type enum Option<T> with [Eq, Clone, Debug] {
Some(T);
None;
}A value of type T that may be absent: Some(value) or None.
Option::Some(T)new T { field: value }
new Path(args)
new E::Variant
new T[n]Heap allocation. new T { .. } places a struct literal on the heap and yields a T*, and new E::V does the same for a unit variant. new Path(args) places the value a call returns, whatever its type: new Table::new(8), new Option<i64>::Some(41), new make_pair(3, 4). new T[n] reserves n uninitialised T - the typed malloc. The storage comes from the value type's Allocate implementation, or else the global allocator. A type is not a function, so new Point(1, 2) is refused (E0231).