Skip to content

Commit dc6c6fb

Browse files
additions
1 parent b4d8afa commit dc6c6fb

File tree

1 file changed

+54
-69
lines changed

1 file changed

+54
-69
lines changed

_posts/2018-08-02-Rust-1.28.md

+54-69
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ appropriate page on our website, and check out the [detailed release notes for
2525

2626
### Global Allocators
2727

28-
1.28.0 adds the `#[global_allocator]` attribute, which allows Rust programs to
29-
set their allocator, as well as define new allocators by implementing the
30-
[`GlobalAlloc`] trait.
28+
Allocators are the way that programs in Rust obtain memory from the system at
29+
runtime. Previously, Rust did not allow changing the way memory is obtained,
30+
which prevented some use cases. With 1.28.0, the `#[global_allocator]` attribute
31+
is now stable, which allows Rust programs to set their allocator to the system
32+
allocator, as well as define new allocators by implementing the [`GlobalAlloc`]
33+
trait.
3134

32-
By default, Rust uses jemalloc on Linux systems, but this may not be ideal for
33-
specific use cases. The standard library provides a method for switching to the
34-
system allocator:
35+
The standard library provides a handle to the system allocator, which can be
36+
used as such:
3537

3638
```rust
3739
use std::alloc::System;
@@ -50,74 +52,57 @@ However, sometimes you want to define a custom allocator for a given application
5052
domain. This is also relatively easy to do by implementing the `GlobalAlloc`
5153
trait. You can read more about how to do this in the [documentation].
5254

53-
[`GlobalAlloc`]: https://doc.rust-lang.org/1.28.0/std/alloc/trait.GlobalAlloc.html
54-
[documentation]: https://doc.rust-lang.org/1.28.0/std/alloc/trait.GlobalAlloc.html
55+
[`GlobalAlloc`]: https://doc.rust-lang.org/stable/std/alloc/trait.GlobalAlloc.html
56+
[documentation]: https://doc.rust-lang.org/stable/std/alloc/trait.GlobalAlloc.html
5557

56-
### Library stabilizations
58+
### Improved error message for formatting
59+
60+
Work on diagnostics continues, this time with an emphasis on formatting:
61+
62+
```rust
63+
format!("{_foo}", _foo = 6usize);
64+
```
65+
66+
Previously, the error message emitted here was relatively poor:
67+
68+
```
69+
error: invalid format string: expected `'}'`, found `'_'`
70+
|
71+
2 | format!("{_foo}", _foo = 6usize);
72+
| ^^^^^^^^
73+
```
5774

58-
Several new APIs were stabilized this release:
59-
60-
- [`Iterator::step_by`]
61-
- [`Path::ancestors`]
62-
- [`SystemTime::UNIX_EPOCH`]
63-
- [`alloc::GlobalAlloc`]
64-
- [`alloc::Layout`]
65-
- [`alloc::LayoutErr`]
66-
- [`alloc::System`]
67-
- [`alloc::alloc`]
68-
- [`alloc::alloc_zeroed`]
69-
- [`alloc::dealloc`]
70-
- [`alloc::realloc`]
71-
- [`alloc::handle_alloc_error`]
72-
- [`btree_map::Entry::or_default`]
73-
- [`fmt::Alignment`]
74-
- [`hash_map::Entry::or_default`]
75-
- [`iter::repeat_with`]
76-
- [`num::NonZeroUsize`]
77-
- [`num::NonZeroU128`]
78-
- [`num::NonZeroU16`]
79-
- [`num::NonZeroU32`]
80-
- [`num::NonZeroU64`]
81-
- [`num::NonZeroU8`]
82-
- [`ops::RangeBounds`]
83-
- [`slice::SliceIndex`]
84-
- [`slice::from_mut`]
85-
- [`slice::from_ref`]
86-
- [`{Any + Send + Sync}::downcast_mut`]
87-
- [`{Any + Send + Sync}::downcast_ref`]
88-
- [`{Any + Send + Sync}::is`]
75+
Now, we emit a diagnostic that tells you the specific reason the format string
76+
is invalid:
77+
78+
```
79+
error: invalid format string: invalid argument name `_foo`
80+
|
81+
2 | let _ = format!("{_foo}", _foo = 6usize);
82+
| ^^^^ invalid argument name in format string
83+
|
84+
= note: argument names cannot start with an underscore
85+
```
8986

9087
See the [detailed release notes][notes] for more.
9188

92-
[`Iterator::step_by`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.step_by
93-
[`Path::ancestors`]: https://doc.rust-lang.org/std/path/struct.Path.html#method.ancestors
94-
[`SystemTime::UNIX_EPOCH`]: https://doc.rust-lang.org/std/time/struct.SystemTime.html#associatedconstant.UNIX_EPOCH
95-
[`alloc::GlobalAlloc`]: https://doc.rust-lang.org/std/alloc/trait.GlobalAlloc.html
96-
[`alloc::Layout`]: https://doc.rust-lang.org/std/alloc/struct.Layout.html
97-
[`alloc::LayoutErr`]: https://doc.rust-lang.org/std/alloc/struct.LayoutErr.html
98-
[`alloc::System`]: https://doc.rust-lang.org/std/alloc/struct.System.html
99-
[`alloc::alloc`]: https://doc.rust-lang.org/std/alloc/fn.alloc.html
100-
[`alloc::alloc_zeroed`]: https://doc.rust-lang.org/std/alloc/fn.alloc_zeroed.html
101-
[`alloc::dealloc`]: https://doc.rust-lang.org/std/alloc/fn.dealloc.html
102-
[`alloc::realloc`]: https://doc.rust-lang.org/std/alloc/fn.realloc.html
103-
[`alloc::handle_alloc_error`]: https://doc.rust-lang.org/std/alloc/fn.handle_alloc_error.html
104-
[`btree_map::Entry::or_default`]: https://doc.rust-lang.org/std/collections/btree_map/enum.Entry.html#method.or_default
105-
[`fmt::Alignment`]: https://doc.rust-lang.org/std/fmt/enum.Alignment.html
106-
[`hash_map::Entry::or_default`]: https://doc.rust-lang.org/std/collections/btree_map/enum.Entry.html#method.or_default
107-
[`iter::repeat_with`]: https://doc.rust-lang.org/std/iter/fn.repeat_with.html
108-
[`num::NonZeroUsize`]: https://doc.rust-lang.org/std/num/struct.NonZeroUsize.html
109-
[`num::NonZeroU128`]: https://doc.rust-lang.org/std/num/struct.NonZeroU128.html
110-
[`num::NonZeroU16`]: https://doc.rust-lang.org/std/num/struct.NonZeroU16.html
111-
[`num::NonZeroU32`]: https://doc.rust-lang.org/std/num/struct.NonZeroU32.html
112-
[`num::NonZeroU64`]: https://doc.rust-lang.org/std/num/struct.NonZeroU64.html
113-
[`num::NonZeroU8`]: https://doc.rust-lang.org/std/num/struct.NonZeroU8.html
114-
[`ops::RangeBounds`]: https://doc.rust-lang.org/std/ops/trait.RangeBounds.html
115-
[`slice::SliceIndex`]: https://doc.rust-lang.org/std/slice/trait.SliceIndex.html
116-
[`slice::from_mut`]: https://doc.rust-lang.org/std/slice/fn.from_mut.html
117-
[`slice::from_ref`]: https://doc.rust-lang.org/std/slice/fn.from_ref.html
118-
[`{Any + Send + Sync}::downcast_mut`]: https://doc.rust-lang.org/std/any/trait.Any.html#method.downcast_mut-2
119-
[`{Any + Send + Sync}::downcast_ref`]: https://doc.rust-lang.org/std/any/trait.Any.html#method.downcast_ref-2
120-
[`{Any + Send + Sync}::is`]: https://doc.rust-lang.org/std/any/trait.Any.html#method.is-2
89+
### Library stabilizations
90+
91+
We've already mentioned the stabilization of the `GlobalAlloc` trait, but
92+
another important stabilization is the [`NonZero`] types. These are wrappers
93+
around the standard unsigned integer types: `u8`, `u16, `u32`, `u64`, `u128`,
94+
and `usize`.
95+
96+
This allows for size-optimization, for example, `Option<u8>` is two bytes large,
97+
but `Option<NonZeroU8>` is just one byte large. This is especially useful when
98+
the number represents an identifier and so you can guarantee that it is never
99+
going to be zero, reducing the size of optional identifiers at no visible cost
100+
to them.
101+
102+
A number of other libraries have also been stabilized: you can see the more
103+
[detailed release notes][notes] for full details.
104+
105+
[`NonZero`]: https://doc.rust-lang.org/stable/std/num/index.html
121106

122107
### Cargo features
123108

0 commit comments

Comments
 (0)