This PR moves almost all our current uses of closures, both in public API and internal uses, to the new "unboxed" closures system. In most cases, downstream code that *only uses* closures will continue to work as it is. The reason is that the `|| {}` syntax can be inferred either as a boxed or an "unboxed" closure according to the context. For example the following code will continue to work: ``` rust some_option.map(|x| x.transform_with(upvar)) ``` And will get silently upgraded to an "unboxed" closure. In some other cases, it may be necessary to "annotate" which `Fn*` trait the closure implements: ``` // Change this |x| { /* body */} // to either of these |: x| { /* body */} // closure implements the FnOnce trait |&mut : x| { /* body */} // FnMut |&: x| { /* body */} // Fn ``` This mainly occurs when the closure is assigned to a variable first, and then passed to a function/method. ``` rust let closure = |: x| x.transform_with(upvar); some.option.map(closure) ``` (It's very likely that in the future, an improved inference engine will make this annotation unnecessary) Other cases that require annotation are closures that implement some trait via a blanket `impl`, for example: - `std::finally::Finally` - `regex::Replacer` - `std::str::CharEq` ``` rust string.trim_left_chars(|c: char| c.is_whitespace()) //~^ ERROR: the trait `Fn<(char,), bool>` is not implemented for the type `|char| -> bool` string.trim_left_chars(|&: c: char| c.is_whitespace()) // OK ``` Finally, all implementations of traits that contain boxed closures in the arguments of their methods are now broken. And will need to be updated to use unboxed closures. These are the main affected traits: - `serialize::Decoder` - `serialize::DecoderHelpers` - `serialize::Encoder` - `serialize::EncoderHelpers` - `rustrt::ToCStr` For example, change this: ``` rust // libserialize/json.rs impl<'a> Encoder<io::IoError> for Encoder<'a> { fn emit_enum(&mut self, _name: &str, f: |&mut Encoder<'a>| -> EncodeResult) -> EncodeResult { f(self) } } ``` to: ``` rust // libserialize/json.rs impl<'a> Encoder<io::IoError> for Encoder<'a> { fn emit_enum<F>(&mut self, _name: &str, f: F) -> EncodeResult where F: FnOnce(&mut Encoder<'a>) -> EncodeResult { f(self) } } ``` [breaking-change] --- ### How the `Fn*` bound has been selected I've chosen the bounds to make the functions/structs as "generic as possible", i.e. to let them allow the maximum amount of input. - An `F: FnOnce` bound accepts the three kinds of closures: `|:|`, `|&mut:|` and `|&:|`. - An `F: FnMut` bound only accepts "non-consuming" closures: `|&mut:|` and `|&:|`. - An `F: Fn` bound only accept the "immutable environment" closures: `|&:|`. This means that whenever possible the `FnOnce` bound has been used, if the `FnOnce` bound couldn't be used, then the `FnMut` was used. The `Fn` bound was never used in the whole repository. The `FnMut` bound was the most used, because it resembles the semantics of the current boxed closures: the closure can modify its environment, and the closure may be called several times. The `FnOnce` bound allows new semantics: you can move out the upvars when the closure is called. This can be effectively paired with the `move || {}` syntax to transfer ownership from the environment to the closure caller. In the case of trait methods, is hard to select the "right" bound since we can't control how the trait may be implemented by downstream users. In these cases, I have selected the bound based on how we use these traits in the repository. For this reason the selected bounds may not be ideal, and may require tweaking before stabilization. r? @aturon
The Rust Programming Language
This is a compiler for Rust, including standard libraries, tools and documentation.
Quick Start
- Download a binary installer for your platform.
- Read the guide.
- Enjoy!
Note: Windows users can read the detailed using Rust on Windows notes on the wiki.
Building from Source
-
Make sure you have installed the dependencies:
g++
4.7 orclang++
3.xpython
2.6 or later (but not 3.x)perl
5.0 or later- GNU
make
3.81 or later curl
git
-
Download and build Rust:
You can either download a tarball or build directly from the repo.
To build from the tarball do:
$ curl -O https://static.rust-lang.org/dist/rust-nightly.tar.gz $ tar -xzf rust-nightly.tar.gz $ cd rust-nightly
Or to build from the repo do:
$ git clone https://github.com/rust-lang/rust.git $ cd rust
Now that you have Rust's source code, you can configure and build it:
$ ./configure $ make && make install
Note: You may need to use
sudo make install
if you do not normally have permission to modify the destination directory. The install locations can be adjusted by passing a--prefix
argument toconfigure
. Various other options are also supported, pass--help
for more information on them.When complete,
make install
will place several programs into/usr/local/bin
:rustc
, the Rust compiler, andrustdoc
, the API-documentation tool. -
Read the guide.
-
Enjoy!
Building on Windows
To easily build on windows we can use MSYS2:
-
Grab the latest MSYS2 installer and go through the installer.
-
Now from the MSYS2 terminal we want to install the mingw64 toolchain and the other tools we need.
$ pacman -S mingw-w64-i686-toolchain $ pacman -S base-devel
-
With that now start
mingw32_shell.bat
from where you installed MSYS2 (i.e.C:\msys
). -
From there just navigate to where you have Rust's source code, configure and build it:
$ ./configure $ make && make install
Notes
Since the Rust compiler is written in Rust, it must be built by a precompiled "snapshot" version of itself (made in an earlier state of development). As such, source builds require a connection to the Internet, to fetch snapshots, and an OS that can execute the available snapshot binaries.
Snapshot binaries are currently built and tested on several platforms:
- Windows (7, 8, Server 2008 R2), x86 and x86-64 (64-bit support added in Rust 0.12.0)
- Linux (2.6.18 or later, various distributions), x86 and x86-64
- OSX 10.7 (Lion) or greater, x86 and x86-64
You may find that other platforms work, but these are our officially supported build environments that are most likely to work.
Rust currently needs about 1.5 GiB of RAM to build without swapping; if it hits swap, it will take a very long time to build.
There is a lot more documentation in the wiki.
Getting help and getting involved
The Rust community congregates in a few places:
- StackOverflow - Get help here.
- /r/rust - General discussion.
- discuss.rust-lang.org - For development of the Rust language itself.
License
Rust is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0), with portions covered by various BSD-like licenses.
See LICENSE-APACHE, LICENSE-MIT, and COPYRIGHT for details.