|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Announcing Rust 1.28" |
| 4 | +author: The Rust Core Team |
| 5 | +--- |
| 6 | + |
| 7 | +The Rust team is happy to announce a new version of Rust, 1.28.0. Rust is a |
| 8 | +systems programming language focused on safety, speed, and concurrency. |
| 9 | + |
| 10 | +If you have a previous version of Rust installed via rustup, getting Rust |
| 11 | +1.28.0 is as easy as: |
| 12 | + |
| 13 | +```bash |
| 14 | +$ rustup update stable |
| 15 | +``` |
| 16 | + |
| 17 | +If you don't have it already, you can [get `rustup`][install] from the |
| 18 | +appropriate page on our website, and check out the [detailed release notes for |
| 19 | +1.28.0][notes] on GitHub. |
| 20 | + |
| 21 | +[install]: https://www.rust-lang.org/install.html |
| 22 | +[notes]: https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1280-2018-08-02 |
| 23 | + |
| 24 | +## What's in 1.28.0 stable |
| 25 | + |
| 26 | +### Global Allocators |
| 27 | + |
| 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. On some platforms, this meant using jemalloc, on |
| 31 | +others, the system allocator, but there was no way for users to control this key |
| 32 | +component. With 1.28.0, the `#[global_allocator]` attribute is now stable, which |
| 33 | +allows Rust programs to set their allocator to the system allocator, as well as |
| 34 | +define new allocators by implementing the [`GlobalAlloc`] trait. |
| 35 | + |
| 36 | +The default allocator for Rust programs on some platforms is jemalloc. The |
| 37 | +standard library now provides a handle to the system allocator, which can be |
| 38 | +used to switch to the system allocator when desired, by declaring a static and |
| 39 | +marking it with the `#[global_allocator]` attribute. |
| 40 | + |
| 41 | +```rust |
| 42 | +use std::alloc::System; |
| 43 | + |
| 44 | +#[global_allocator] |
| 45 | +static GLOBAL: System = System; |
| 46 | + |
| 47 | +fn main() { |
| 48 | + let mut v = Vec::new(); |
| 49 | + // This will allocate memory using the system allocator. |
| 50 | + v.push(1); |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +However, sometimes you want to define a custom allocator for a given application |
| 55 | +domain. This is also relatively easy to do by implementing the `GlobalAlloc` |
| 56 | +trait. You can read more about how to do this in the [documentation]. |
| 57 | + |
| 58 | +[`GlobalAlloc`]: https://doc.rust-lang.org/stable/std/alloc/trait.GlobalAlloc.html |
| 59 | +[documentation]: https://doc.rust-lang.org/stable/std/alloc/trait.GlobalAlloc.html |
| 60 | + |
| 61 | +### Improved error message for formatting |
| 62 | + |
| 63 | +Work on diagnostics continues, this time with an emphasis on formatting: |
| 64 | + |
| 65 | +```rust |
| 66 | +format!("{_foo}", _foo = 6usize); |
| 67 | +``` |
| 68 | + |
| 69 | +Previously, the error message emitted here was relatively poor: |
| 70 | + |
| 71 | +``` |
| 72 | +error: invalid format string: expected `'}'`, found `'_'` |
| 73 | + | |
| 74 | +2 | format!("{_foo}", _foo = 6usize); |
| 75 | + | ^^^^^^^^ |
| 76 | +``` |
| 77 | + |
| 78 | +Now, we emit a diagnostic that tells you the specific reason the format string |
| 79 | +is invalid: |
| 80 | + |
| 81 | +``` |
| 82 | +error: invalid format string: invalid argument name `_foo` |
| 83 | + | |
| 84 | +2 | let _ = format!("{_foo}", _foo = 6usize); |
| 85 | + | ^^^^ invalid argument name in format string |
| 86 | + | |
| 87 | + = note: argument names cannot start with an underscore |
| 88 | +``` |
| 89 | + |
| 90 | +See the [detailed release notes][notes] for more. |
| 91 | + |
| 92 | +### Library stabilizations |
| 93 | + |
| 94 | +We've already mentioned the stabilization of the `GlobalAlloc` trait, but |
| 95 | +another important stabilization is the [`NonZero`] number types. These are wrappers |
| 96 | +around the standard unsigned integer types: `NonZeroU8`, `NonZeroU16`, |
| 97 | +`NonZeroU32`, `NonZeroU64`, `NonZeroU128`, and `NonZeroUsize`. |
| 98 | + |
| 99 | +This allows for size optimization, for example, `Option<u8>` is two bytes large, |
| 100 | +but `Option<NonZeroU8>` is just one byte large. Note that this optimization |
| 101 | +remains even when `NonZeroU8` is wrapped inside another struct; the example |
| 102 | +below illustrates that `Door` is still 1 byte large despite being placed inside |
| 103 | +an `Option`. This optimization applies to user-defined enums as well: `Option` |
| 104 | +is not special. |
| 105 | + |
| 106 | +```rust |
| 107 | +use std::mem; |
| 108 | +use std::num::NonZeroU8; |
| 109 | + |
| 110 | +struct Key(NonZeroU8); |
| 111 | + |
| 112 | +struct Door { |
| 113 | + key: Key, |
| 114 | +} |
| 115 | + |
| 116 | +fn main() { |
| 117 | + assert_eq!(mem::size_of::<Door>(), 1); |
| 118 | + assert_eq!(mem::size_of::<Option<Door>>(), 1); |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +A number of other libraries have also been stabilized: you can see the more |
| 123 | +[detailed release notes][notes] for full details. |
| 124 | + |
| 125 | +[`NonZero`]: https://doc.rust-lang.org/stable/std/num/index.html |
| 126 | + |
| 127 | +### Cargo features |
| 128 | + |
| 129 | +[Cargo will now no longer allow you to publish crates with build scripts that |
| 130 | +modify the `src` directory.][cargo/5584] The `src` directory in a crate should be |
| 131 | +considered to be immutable. |
| 132 | + |
| 133 | +[cargo/5584]: https://github.com/rust-lang/cargo/pull/5584/ |
| 134 | + |
| 135 | +## Contributors to 1.28.0 |
| 136 | + |
| 137 | +Many people came together to create Rust 1.28. We couldn't have done it |
| 138 | +without all of you. [Thanks!](https://thanks.rust-lang.org/rust/1.28.0) |
0 commit comments