2014-08-01 18:31:22 -05:00
|
|
|
% The Rust Design FAQ
|
2014-05-23 00:01:44 -05:00
|
|
|
|
2014-08-19 18:31:07 -05:00
|
|
|
This document describes decisions that were arrived at after lengthy discussion and
|
2014-05-23 00:01:44 -05:00
|
|
|
experimenting with alternatives. Please do not propose reversing them unless
|
|
|
|
you have a new, extremely compelling argument. Note that this document
|
|
|
|
specifically talks about the *language* and not any library or implementation.
|
|
|
|
|
|
|
|
A few general guidelines define the philosophy:
|
|
|
|
|
|
|
|
- [Memory safety][mem] must never be compromised
|
|
|
|
- [Abstraction][abs] should be zero-cost, while still maintaining safety
|
|
|
|
- Practicality is key
|
|
|
|
|
|
|
|
[mem]: http://en.wikipedia.org/wiki/Memory_safety
|
|
|
|
[abs]: http://en.wikipedia.org/wiki/Abstraction_%28computer_science%29
|
|
|
|
|
|
|
|
# Semantics
|
|
|
|
|
|
|
|
## Data layout is unspecified
|
|
|
|
|
|
|
|
In the general case, `enum` and `struct` layout is undefined. This allows the
|
|
|
|
compiler to potentially do optimizations like re-using padding for the
|
|
|
|
discriminant, compacting variants of nested enums, reordering fields to remove
|
|
|
|
padding, etc. `enum`s which carry no data ("C-like") are eligible to have a
|
|
|
|
defined representation. Such `enum`s are easily distinguished in that they are
|
|
|
|
simply a list of names that carry no data:
|
|
|
|
|
|
|
|
```
|
|
|
|
enum CLike {
|
|
|
|
A,
|
|
|
|
B = 32,
|
|
|
|
C = 34,
|
|
|
|
D
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The [repr attribute][repr] can be applied to such `enum`s to give them the same
|
|
|
|
representation as a primitive. This allows using Rust `enum`s in FFI where C
|
|
|
|
`enum`s are also used, for most use cases. The attribute can also be applied
|
|
|
|
to `struct`s to get the same layout as a C struct would.
|
|
|
|
|
2015-05-06 04:11:54 -05:00
|
|
|
[repr]: reference.html#ffi-attributes
|
2014-05-23 00:01:44 -05:00
|
|
|
|
|
|
|
## There is no GC
|
|
|
|
|
|
|
|
A language that requires a GC is a language that opts into a larger, more
|
|
|
|
complex runtime than Rust cares for. Rust is usable on bare metal with no
|
|
|
|
extra runtime. Additionally, garbage collection is frequently a source of
|
|
|
|
non-deterministic behavior. Rust provides the tools to make using a GC
|
|
|
|
possible and even pleasant, but it should not be a requirement for
|
|
|
|
implementing the language.
|
|
|
|
|
2014-08-05 18:40:04 -05:00
|
|
|
## Non-`Sync` `static mut` is unsafe
|
2014-05-23 00:01:44 -05:00
|
|
|
|
2014-08-05 18:40:04 -05:00
|
|
|
Types which are [`Sync`][sync] are thread-safe when multiple shared
|
|
|
|
references to them are used concurrently. Types which are not `Sync` are not
|
2014-05-23 00:01:44 -05:00
|
|
|
thread-safe, and thus when used in a global require unsafe code to use.
|
|
|
|
|
2015-04-16 15:12:13 -05:00
|
|
|
[sync]: core/marker/trait.Sync.html
|
2014-05-23 00:01:44 -05:00
|
|
|
|
2014-08-05 18:40:04 -05:00
|
|
|
### If mutable static items that implement `Sync` are safe, why is taking &mut SHARABLE unsafe?
|
2014-05-23 00:01:44 -05:00
|
|
|
|
|
|
|
Having multiple aliasing `&mut T`s is never allowed. Due to the nature of
|
|
|
|
globals, the borrow checker cannot possibly ensure that a static obeys the
|
|
|
|
borrowing rules, so taking a mutable reference to a static is always unsafe.
|
|
|
|
|
|
|
|
## There is no life before or after main (no static ctors/dtors)
|
|
|
|
|
|
|
|
Globals can not have a non-constant-expression constructor and cannot have a
|
|
|
|
destructor at all. This is an opinion of the language. Static constructors are
|
|
|
|
undesirable because they can slow down program startup. Life before main is
|
|
|
|
often considered a misfeature, never to be used. Rust helps this along by just
|
|
|
|
not having the feature.
|
|
|
|
|
|
|
|
See [the C++ FQA][fqa] about the "static initialization order fiasco", and
|
|
|
|
[Eric Lippert's blog][elp] for the challenges in C#, which also has this
|
|
|
|
feature.
|
|
|
|
|
|
|
|
A nice replacement is the [lazy constructor macro][lcm] by [Marvin
|
|
|
|
Löbel][kim].
|
|
|
|
|
2015-04-28 15:45:40 -05:00
|
|
|
[fqa]: http://yosefk.com/c++fqa/ctors.html#fqa-10.12
|
2014-05-23 00:01:44 -05:00
|
|
|
[elp]: http://ericlippert.com/2013/02/06/static-constructors-part-one/
|
|
|
|
[lcm]: https://gist.github.com/Kimundi/8782487
|
|
|
|
[kim]: https://github.com/Kimundi
|
|
|
|
|
|
|
|
## The language does not require a runtime
|
|
|
|
|
|
|
|
See the above entry on GC. Requiring a runtime limits the utility of the
|
|
|
|
language, and makes it undeserving of the title "systems language". All Rust
|
|
|
|
code should need to run is a stack.
|
|
|
|
|
|
|
|
## `match` must be exhaustive
|
|
|
|
|
|
|
|
`match` being exhaustive has some useful properties. First, if every
|
|
|
|
possibility is covered by the `match`, adding further variants to the `enum`
|
2014-10-09 14:17:22 -05:00
|
|
|
in the future will prompt a compilation failure, rather than runtime panic.
|
2014-11-15 17:17:36 -06:00
|
|
|
Second, it makes cost explicit. In general, the only safe way to have a
|
2015-05-08 10:12:29 -05:00
|
|
|
non-exhaustive match would be to panic the thread if nothing is matched, though
|
2014-05-23 00:01:44 -05:00
|
|
|
it could fall through if the type of the `match` expression is `()`. This sort
|
|
|
|
of hidden cost and special casing is against the language's philosophy. It's
|
2015-06-30 15:02:10 -05:00
|
|
|
easy to ignore all unspecified cases by using the `_` wildcard:
|
2014-05-23 00:01:44 -05:00
|
|
|
|
|
|
|
```rust,ignore
|
|
|
|
match val.do_something() {
|
|
|
|
Cat(a) => { /* ... */ }
|
|
|
|
_ => { /* ... */ }
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
[#3101][iss] is the issue that proposed making this the only behavior, with
|
|
|
|
rationale and discussion.
|
|
|
|
|
2014-06-16 18:07:34 -05:00
|
|
|
[iss]: https://github.com/rust-lang/rust/issues/3101
|
2014-05-23 00:01:44 -05:00
|
|
|
|
|
|
|
## No guaranteed tail-call optimization
|
|
|
|
|
2014-06-16 15:56:54 -05:00
|
|
|
In general, tail-call optimization is not guaranteed: see [here][tml] for a
|
|
|
|
detailed explanation with references. There is a [proposed extension][tce] that
|
|
|
|
would allow tail-call elimination in certain contexts. The compiler is still
|
|
|
|
free to optimize tail-calls [when it pleases][sco], however.
|
2014-05-23 00:01:44 -05:00
|
|
|
|
|
|
|
[tml]: https://mail.mozilla.org/pipermail/rust-dev/2013-April/003557.html
|
|
|
|
[sco]: http://llvm.org/docs/CodeGenerator.html#sibling-call-optimization
|
|
|
|
[tce]: https://github.com/rust-lang/rfcs/pull/81
|
|
|
|
|
|
|
|
## No constructors
|
|
|
|
|
|
|
|
Functions can serve the same purpose as constructors without adding any
|
|
|
|
language complexity.
|
|
|
|
|
|
|
|
## No copy constructors
|
|
|
|
|
|
|
|
Types which implement [`Copy`][copy], will do a standard C-like "shallow copy"
|
|
|
|
with no extra work (similar to "plain old data" in C++). It is impossible to
|
|
|
|
implement `Copy` types that require custom copy behavior. Instead, in Rust
|
|
|
|
"copy constructors" are created by implementing the [`Clone`][clone] trait,
|
|
|
|
and explicitly calling the `clone` method. Making user-defined copy operators
|
|
|
|
explicit surfaces the underlying complexity, forcing the developer to opt-in
|
|
|
|
to potentially expensive operations.
|
|
|
|
|
2015-04-16 15:12:13 -05:00
|
|
|
[copy]: core/marker/trait.Copy.html
|
2014-09-13 20:16:11 -05:00
|
|
|
[clone]: core/clone/trait.Clone.html
|
2014-05-23 00:01:44 -05:00
|
|
|
|
|
|
|
## No move constructors
|
|
|
|
|
|
|
|
Values of all types are moved via `memcpy`. This makes writing generic unsafe
|
|
|
|
code much simpler since assignment, passing and returning are known to never
|
|
|
|
have a side effect like unwinding.
|
|
|
|
|
|
|
|
# Syntax
|
|
|
|
|
|
|
|
## Macros require balanced delimiters
|
|
|
|
|
|
|
|
This is to make the language easier to parse for machines. Since the body of a
|
|
|
|
macro can contain arbitrary tokens, some restriction is needed to allow simple
|
|
|
|
non-macro-expanding lexers and parsers. This comes in the form of requiring
|
|
|
|
that all delimiters be balanced.
|
|
|
|
|
|
|
|
## `->` for function return type
|
|
|
|
|
|
|
|
This is to make the language easier to parse for humans, especially in the face
|
2015-05-27 18:20:32 -05:00
|
|
|
of higher-order functions. `fn foo<T>(f: fn(i32): i32, fn(T): U): U` is not
|
2014-05-23 00:01:44 -05:00
|
|
|
particularly easy to read.
|
|
|
|
|
2015-04-07 21:29:11 -05:00
|
|
|
## Why is `let` used to introduce variables?
|
2014-05-23 00:01:44 -05:00
|
|
|
|
2015-04-18 10:36:41 -05:00
|
|
|
Instead of the term "variable", we use "variable bindings". The
|
|
|
|
simplest way for creating a binding is by using the `let` syntax.
|
|
|
|
Other ways include `if let`, `while let`, and `match`. Bindings also
|
|
|
|
exist in function argument positions.
|
2015-04-07 21:29:11 -05:00
|
|
|
|
|
|
|
Bindings always happen in pattern matching positions, and it's also Rust's way
|
2015-04-18 10:36:41 -05:00
|
|
|
to declare mutability. One can also re-declare mutability of a binding in
|
2015-04-07 21:29:11 -05:00
|
|
|
pattern matching. This is useful to avoid unnecessary `mut` annotations. An
|
|
|
|
interesting historical note is that Rust comes, syntactically, most closely
|
|
|
|
from ML, which also uses `let` to introduce bindings.
|
2014-05-23 00:01:44 -05:00
|
|
|
|
|
|
|
See also [a long thread][alt] on renaming `let mut` to `var`.
|
|
|
|
|
|
|
|
[alt]: https://mail.mozilla.org/pipermail/rust-dev/2014-January/008319.html
|
2015-03-08 09:34:00 -05:00
|
|
|
|
|
|
|
## Why no `--x` or `x++`?
|
|
|
|
|
|
|
|
Preincrement and postincrement, while convenient, are also fairly complex. They
|
|
|
|
require knowledge of evaluation order, and often lead to subtle bugs and
|
|
|
|
undefined behavior in C and C++. `x = x + 1` or `x += 1` is only slightly
|
|
|
|
longer, but unambiguous.
|