Skip to content

Commit 0a8ab50

Browse files
authored
Merge pull request #213 from rust-lang/gh201
Changes for Rust 1.32 & setup for edition-next
2 parents 4927074 + beed7d4 commit 0a8ab50

File tree

8 files changed

+222
-0
lines changed

8 files changed

+222
-0
lines changed

src/SUMMARY.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,13 @@
8585
- [MSVC toolchain support](rust-2018/platform-and-target-support/msvc-toolchain-support.md)
8686
- [MUSL support for fully static binaries](rust-2018/platform-and-target-support/musl-support-for-fully-static-binaries.md)
8787
- [`cdylib` crates for C interoperability](rust-2018/platform-and-target-support/cdylib-crates-for-c-interoperability.md)
88+
89+
## The Next Edition
90+
91+
- [The Next Edition](rust-next/index.md)
92+
- [Next-Specific Changes](rust-next/edition-changes.md)
93+
- [The dbg! macro](rust-next/dbg-macro.md)
94+
- [No jemalloc by default](rust-next/no-jemalloc.md)
95+
- [Uniform Paths](rust-next/uniform-paths.md)
96+
- [`literal` macro matcher](rust-next/literal-macro-matcher.md)
97+
- [`?` operator in macros](rust-next/qustion-mark-operator-in-macros.md)

src/rust-next/dbg-macro.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# The dbg! macro
2+
3+
![Minimum Rust version: 1.32](https://img.shields.io/badge/Minimum%20Rust%20Version-1.32-brightgreen.svg)
4+
5+
The `dbg!` macro provides a nicer experience for debugging than `println!`:
6+
7+
```rust
8+
fn main() {
9+
let x = 5;
10+
11+
dbg!(x);
12+
}
13+
```
14+
15+
If you run this program, you'll see:
16+
17+
```text
18+
[src/main.rs:4] x = 5
19+
```
20+
21+
You get the file and line number of where this was invoked, as well as the
22+
name and value. Additionally, `println!` prints to the standard output, so you
23+
really should be using `eprintln!` to print to standard error. `dbg!` does the
24+
right thing and goes to stderr.
25+
26+
It even works in more complex circumstances. Consider this factorial example:
27+
28+
```rust
29+
fn factorial(n: u32) -> u32 {
30+
if n <= 1 {
31+
n
32+
} else {
33+
n * factorial(n - 1)
34+
}
35+
}
36+
```
37+
38+
If we wanted to debug this, we might write it like this with `eprintln!`:
39+
40+
```rust
41+
fn factorial(n: u32) -> u32 {
42+
eprintln!("n: {}", n);
43+
44+
if n <= 1 {
45+
eprintln!("n <= 1");
46+
47+
n
48+
} else {
49+
let n = n * factorial(n - 1);
50+
51+
eprintln!("n: {}", n);
52+
53+
n
54+
}
55+
}
56+
```
57+
58+
We want to log `n` on each iteration, as well as have some kind of context
59+
for each of the branches. We see this output for `factorial(4)`:
60+
61+
```text
62+
n: 4
63+
n: 3
64+
n: 2
65+
n: 1
66+
n <= 1
67+
n: 2
68+
n: 6
69+
n: 24
70+
```
71+
72+
This is servicable, but not particularly great. Maybe we could work on how we
73+
print out the context to make it more clear, but now we're not debugging our
74+
code, we're figuring out how to make our debugging code better.
75+
76+
Consider this version using `dbg!`:
77+
78+
```rust
79+
fn factorial(n: u32) -> u32 {
80+
if dbg!(n <= 1) {
81+
dbg!(1)
82+
} else {
83+
dbg!(n * factorial(n - 1))
84+
}
85+
}
86+
```
87+
88+
We simply wrap each of the various expressions we want to print with the macro. We get this output instead:
89+
90+
```text
91+
[src/main.rs:3] n <= 1 = false
92+
[src/main.rs:3] n <= 1 = false
93+
[src/main.rs:3] n <= 1 = false
94+
[src/main.rs:3] n <= 1 = true
95+
[src/main.rs:4] 1 = 1
96+
[src/main.rs:5] n * factorial(n - 1) = 2
97+
[src/main.rs:5] n * factorial(n - 1) = 6
98+
[src/main.rs:5] n * factorial(n - 1) = 24
99+
[src/main.rs:11] factorial(4) = 24
100+
```
101+
102+
Because the `dbg!` macro returns the value of what it's debugging, instead of
103+
`eprintln!` which returns `()`, we need to make no changes to the structure of
104+
our code. Additionally, we have vastly more useful output.

src/rust-next/edition-changes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Next-Specific Changes
2+
3+
There have been no specific changes accepted for the next edition yet.

src/rust-next/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# The Next Edition
2+
3+
We have not decided if and when the next edition will ship; there is talk of
4+
a 2021 edition to keep up the three-year schedule, but that has not been
5+
formally decided.
6+
7+
Until we do, this section keeps track of changes that landed after Rust 2018.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# literal macro matcher
2+
3+
![Minimum Rust version: 1.32](https://img.shields.io/badge/Minimum%20Rust%20Version-1.32-brightgreen.svg)
4+
5+
A new `literal` matcher was added for macros:
6+
7+
```rust
8+
macro_rules! m {
9+
($lt:literal) => {};
10+
}
11+
12+
fn main() {
13+
m!("some string literal");
14+
}
15+
```
16+
17+
`literal` matches against literals of any type; string literals, numeric
18+
literals, `char` literals.

src/rust-next/no-jemalloc.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# No jemalloc by default
2+
3+
![Minimum Rust version: 1.32](https://img.shields.io/badge/Minimum%20Rust%20Version-1.32-brightgreen.svg)
4+
5+
Long, long ago, Rust had a large, Erlang-like runtime. We chose to use
6+
jemalloc instead of the system allocator, because it often improved
7+
performance over the default system one. Over time, we shed more and more of
8+
this runtime, and eventually almost all of it was removed, but jemalloc was
9+
not. We didn't have a way to choose a custom allocator, and so we couldn't
10+
really remove it without causing a regression for people who do need
11+
jemalloc.
12+
13+
Also, saying that jemalloc was always the default is a bit UNIX-centric, as
14+
it was only the default on some platforms. Notably, the MSVC target on
15+
Windows has shipped the system allocator for a long time.
16+
17+
While jemalloc usually has great performance, that's not always the case.
18+
Additionally, it adds about 300kb to every Rust binary. We've also had a host
19+
of other issues with jemalloc in the past. It has also felt a little strange
20+
that a systems language does not default to the system's allocator.
21+
22+
For all of these reasons, once Rust 1.28 shipped a way to choose a global
23+
allocator, we started making plans to switch the default to the system
24+
allocator, and allow you to use jemalloc via a crate. In Rust 1.32, we've
25+
finally finished this work, and by default, you will get the system allocator
26+
for your programs.
27+
28+
If you'd like to continue to use jemalloc, use the jemallocator crate. In
29+
your Cargo.toml:
30+
31+
```toml
32+
jemallocator = "0.1.8"
33+
```
34+
35+
And in your crate root:
36+
37+
```rust,ignore
38+
#[global_allocator]
39+
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
40+
```
41+
42+
That's it! If you don't need jemalloc, it's not forced upon you, and if you
43+
do need it, it's a few lines of code away.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# ? operator in macros
2+
3+
![Minimum Rust version: 1.32](https://img.shields.io/badge/Minimum%20Rust%20Version-1.32-brightgreen.svg)
4+
5+
`macro_rules` macros can use `?`, like this:
6+
7+
```rust
8+
macro_rules! bar {
9+
($(a)?) => {}
10+
}
11+
```
12+
13+
The `?` will match zero or one repetitions of the pattern, similar to the
14+
already-existing `*` for "zero or more" and `+` for "one or more."

src/rust-next/uniform-paths.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Uniform Paths
2+
3+
![Minimum Rust version: 1.32](https://img.shields.io/badge/Minimum%20Rust%20Version-1.32-brightgreen.svg)
4+
5+
Rust 2018 added several improvements to the module system. We have one last
6+
tweak landing in 1.32.0. Nicknamed "uniform paths", it permits previously
7+
invalid import path statements to be resolved exactly the same way as
8+
non-import paths. For example:
9+
10+
```rust,edition2018
11+
enum Color {
12+
Red,
13+
Green,
14+
Blue,
15+
}
16+
17+
use Color::*;
18+
```
19+
20+
This code did not previously compile, as use statements had to start with
21+
`super`, `self`, or `crate`. Now that the compiler supports uniform paths,
22+
this code will work, and do what you probably expect: import the variants of
23+
the Color enum defined above the `use` statement.

0 commit comments

Comments
 (0)