Skip to content

Commit 561d393

Browse files
committed
Announcing Rust 1.32.0
1 parent dab10d5 commit 561d393

File tree

1 file changed

+274
-0
lines changed

1 file changed

+274
-0
lines changed

posts/2019-01-17-Rust-1.32.0.md

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
---
2+
layout: post
3+
title: "Announcing Rust 1.32.0"
4+
author: The Rust Release Team
5+
---
6+
7+
The Rust team is happy to announce a new version of Rust, 1.32.0. Rust is a
8+
programming language that is empowering everyone to build reliable and
9+
efficient software.
10+
11+
If you have a previous version of Rust installed via rustup, getting Rust
12+
1.32.0 is as easy as:
13+
14+
```
15+
$ rustup update stable
16+
```
17+
18+
If you don't have it already, you can [get `rustup`][install] from the
19+
appropriate page on our website, and check out the [detailed release notes for
20+
1.32.0][notes] on GitHub.
21+
22+
[install]: https://www.rust-lang.org/install.html
23+
[notes]: https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1320-2019-01-17
24+
25+
## What's in 1.32.0 stable
26+
27+
Rust 1.32.0 has a few quality of life improvements, switches the default
28+
allocator, and makes additional functions `const`. Read on for a few
29+
highlights, or see the [detailed release notes][notes] for additional
30+
information.
31+
32+
#### The `dbg` macro
33+
34+
First up, a small quality of life improvement. Are you a "printf debugger"? If you are, and
35+
you've wanted to print out some value while working on some code, you have to do this:
36+
37+
```rust
38+
let x = 5;
39+
40+
println!("{:?}", x);
41+
42+
// or maybe even this
43+
println!("{:#?}", x);
44+
```
45+
46+
This isn't the *largest* speed bump, but it is a lot of stuff to simply show the value of `x`.
47+
Additionally, there's no context here. If you have several of these `println!`s, it can be hard
48+
to tell which is which, unless you add your own context to each invocation, causing even more work.
49+
50+
In Rust 1.32.0, [we've added a new macro,
51+
`dbg!`](https://github.com/rust-lang/rust/pull/56395/), for this purpose:
52+
53+
```rust
54+
fn main() {
55+
let x = 5;
56+
57+
dbg!(x);
58+
}
59+
```
60+
61+
If you run this program, you'll see:
62+
63+
```text
64+
[src/main.rs:4] x = 5
65+
```
66+
67+
You get the file and line number of where this was invoked, as well as the
68+
name and value. Additionally, `println!` prints to the standard output, so
69+
you really should be using `eprintln!` to print to standard error. `dbg!`
70+
does the right thing and goes to `stderr` by default.
71+
72+
It even works in more complex circumstances. Consider this factorial example:
73+
74+
```rust
75+
fn factorial(n: u32) -> u32 {
76+
if n <= 1 {
77+
n
78+
} else {
79+
n * factorial(n - 1)
80+
}
81+
}
82+
```
83+
84+
If we wanted to debug this, we might write it like this with `eprintln!`:
85+
86+
```rust
87+
fn factorial(n: u32) -> u32 {
88+
eprintln!("n: {}", n);
89+
90+
if n <= 1 {
91+
eprintln!("n <= 1");
92+
93+
n
94+
} else {
95+
let n = n * factorial(n - 1);
96+
97+
eprintln!("n: {}", n);
98+
99+
n
100+
}
101+
}
102+
```
103+
104+
We want to log `n` on each iteration, as well as have some kind of context
105+
for each of the branches. We see this output for `factorial(4)`:
106+
107+
```text
108+
n: 4
109+
n: 3
110+
n: 2
111+
n: 1
112+
n <= 1
113+
n: 2
114+
n: 6
115+
n: 24
116+
```
117+
118+
This is servicable, but not particularly great. Maybe we could work on how we
119+
print out the context to make it more clear, but now we're not debugging our code,
120+
we're figuring out how to make our debugging code better.
121+
122+
Consider this version using `dbg!`:
123+
124+
```rust
125+
fn factorial(n: u32) -> u32 {
126+
if dbg!(n <= 1) {
127+
dbg!(1)
128+
} else {
129+
dbg!(n * factorial(n - 1))
130+
}
131+
}
132+
```
133+
134+
We simply wrap each of the various expressions we want to print with the macro. We
135+
get this output instead:
136+
137+
```text
138+
[src/main.rs:3] n <= 1 = false
139+
[src/main.rs:3] n <= 1 = false
140+
[src/main.rs:3] n <= 1 = false
141+
[src/main.rs:3] n <= 1 = true
142+
[src/main.rs:4] 1 = 1
143+
[src/main.rs:5] n * factorial(n - 1) = 2
144+
[src/main.rs:5] n * factorial(n - 1) = 6
145+
[src/main.rs:5] n * factorial(n - 1) = 24
146+
[src/main.rs:11] factorial(4) = 24
147+
```
148+
149+
Because the `dbg!` macro returns the value of what it's debugging, instead of
150+
`eprintln!` which returns `()`, we need to make *no* changes to the structure
151+
of our code. Additionally, we have *vastly* more useful output.
152+
153+
That's a lot to say about a little macro, but we hope it improves your
154+
debugging experience! We are contining to work on support for `gdb` and
155+
friends as well, of course.
156+
157+
#### `jemalloc` is removed by default
158+
159+
Long, long ago, Rust had a large, Erlang-like runtime. We chose to use
160+
[jemalloc] instead of the system allocator, because it often improved
161+
performance over the default system one. Over time, we shed more and more of
162+
this runtime, and eventually almost all of it was removed, but jemalloc
163+
didn't. We didn't have a way to choose a custom allocator, and so we couldn't
164+
really remove it without causing a regression for people who do need
165+
jemalloc.
166+
167+
Also, saying that `jemalloc` was always the default is a bit UNIX-centric,
168+
as it was only the default on *some* platforms. Notably, the MSVC target on
169+
Windows has shipped the system allocator for a long time.
170+
171+
Finally, while jemalloc *usually* has great performance, that's not always
172+
the case. Additionally, it adds about 300kb to every Rust binary. We've also
173+
had a host of [other
174+
issues](https://github.com/rust-lang/rust/issues/36963#issuecomment-252029017)
175+
with jemalloc in the past. It has also felt a little strange that a systems
176+
language does not default to the system's allocator.
177+
178+
For all of these reasons, once [Rust 1.28 shipped a way to choose a global
179+
allocator](https://blog.rust-lang.org/2018/08/02/Rust-1.28.html#whats-in-1.28.0-stable),
180+
we started making plans to switch the default to the system allocator, and
181+
allow you to use `jemalloc` via a crate. In Rust 1.32, we've finally finished
182+
this work, and by default, you will get the system allocator for your
183+
programs.
184+
185+
If you'd like to continue to use jemalloc, use [the jemallocator crate]. In
186+
your `Cargo.toml`:
187+
188+
```toml
189+
jemallocator = "0.1.8"
190+
```
191+
192+
And in your crate root:
193+
194+
```rust
195+
#[global_allocator]
196+
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
197+
```
198+
199+
That's it! If you don't need jemalloc, it's not forced upon you, and if
200+
you do need it, it's a few lines of code away.
201+
202+
[jemalloc]: http://jemalloc.net/
203+
[the jemallocator crate]: https://crates.io/crates/jemallocator
204+
205+
#### Macro improvements
206+
207+
A few improvements to macros have landed in Rust 1.32.0. First, [a new
208+
`literal` matcher](https://github.com/rust-lang/rust/pull/56072/) was added:
209+
210+
```rust
211+
macro_rules! m {
212+
($lt:literal) => {};
213+
}
214+
215+
fn main() {
216+
m!("some string literal");
217+
}
218+
```
219+
220+
`literal` mactches against literals of any type; string literals, numeric literals, `char` literals.
221+
222+
In the 2018 edition, `macro_rules` macros can also use `?`, like this:
223+
224+
```rust
225+
macro_rules! bar {
226+
($(a)?) => {}
227+
}
228+
```
229+
230+
The `?` will match zero or one repitions of the pattern, similar to the
231+
already-existing `*` for "zero or more" and `+` for "one or more."
232+
233+
#### Final module improvements
234+
235+
In Rust 1.30.0, [we announced several improvements to the module
236+
system](https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html#module-system-changes).
237+
We have one last tweak landing in 1.32.0 and the 2018 edition. Nicknamed
238+
["uniform paths"](https://github.com/rust-lang/rust/pull/56759/), the simplest way to explain
239+
it is to show you this code:
240+
241+
```rust
242+
enum Color { Red, Green, Blue }
243+
244+
use Color::*;
245+
```
246+
247+
This code did *not* previously compile, as `use` statements had to start with
248+
`super`, `self`, or `crate`. With this change, this code will work, and do
249+
what you probably expect: import the variants of the `Color` enum defined
250+
above the `use` statement.
251+
252+
With this change in place, we've completed our efforts at revising the module
253+
system. We hope you've been enjoying the simplified system so far!
254+
255+
### Library stabilizations
256+
257+
We talked above about the `dbg!` macro, which is a big library addition.
258+
Beyond that, 19 functions were made `const fn`s, and all 72 combinations of
259+
the `to_*_bytes` and `from_*_bytes` methods, where `*` is one of `{be, le,
260+
ne}`, were added to `{usize,isize,i8,i16,i32,i64,i128,u8,u16,u32,u64,u128}`.
261+
See the [detailed release notes][notes] for more details.
262+
263+
### Cargo features
264+
265+
Cargo gained [`cargo c` as an alias for `cargo
266+
check`](https://github.com/rust-lang/cargo/pull/6218/), and now [allows
267+
usernames in registry URLs](https://github.com/rust-lang/cargo/pull/6242/).
268+
269+
See the [detailed release notes][notes] for more.
270+
271+
## Contributors to 1.32.0
272+
273+
Many people came together to create Rust 1.32.0. We couldn't have done it
274+
without all of you. [Thanks!](https://thanks.rust-lang.org/rust/1.32.0)

0 commit comments

Comments
 (0)