|
| 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. 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. |
| 34 | + |
| 35 | +The standard library provides a handle to the system allocator, which can be |
| 36 | +used as such: |
| 37 | + |
| 38 | +```rust |
| 39 | +use std::alloc::System; |
| 40 | + |
| 41 | +#[global_allocator] |
| 42 | +static GLOBAL: System = System; |
| 43 | + |
| 44 | +fn main() { |
| 45 | + let mut v = Vec::new(); |
| 46 | + // This will allocate memory using the system allocator. |
| 47 | + v.push(1); |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +However, sometimes you want to define a custom allocator for a given application |
| 52 | +domain. This is also relatively easy to do by implementing the `GlobalAlloc` |
| 53 | +trait. You can read more about how to do this in the [documentation]. |
| 54 | + |
| 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 |
| 57 | + |
| 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 | +``` |
| 74 | + |
| 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 | +``` |
| 86 | + |
| 87 | +See the [detailed release notes][notes] for more. |
| 88 | + |
| 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 |
| 106 | + |
| 107 | +### Cargo features |
| 108 | + |
| 109 | +[Cargo will now no longer allow you to publish crates with build scripts that |
| 110 | +modify the `src` directory.][cargo/5584] The `src` directory in a crate should be |
| 111 | +considered to be immutable. |
| 112 | + |
| 113 | +[cargo/5584]: https://github.com/rust-lang/cargo/pull/5584/ |
| 114 | + |
| 115 | +## Contributors to 1.28.0 |
| 116 | + |
| 117 | +Many people came together to create Rust 1.28. We couldn't have done it |
| 118 | +without all of you. [Thanks!](https://thanks.rust-lang.org/rust/1.28.0) |
0 commit comments