|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Announcing Rust 1.18" |
| 4 | +author: The Rust Core Team |
| 5 | +--- |
| 6 | + |
| 7 | +The Rust team is happy to announce the latest version of Rust, 1.18.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, getting Rust 1.18 is as easy as: |
| 11 | + |
| 12 | +```bash |
| 13 | +$ rustup update stable |
| 14 | +``` |
| 15 | + |
| 16 | +If you don't have it already, you can [get `rustup`][install] from the |
| 17 | +appropriate page on our website, and check out the [detailed release notes for |
| 18 | +1.18.0][notes] on GitHub. |
| 19 | + |
| 20 | +[install]: https://www.rust-lang.org/install.html |
| 21 | +[notes]: https://github.com/rust-lang/rust/blob/rust-1.17-relnotes/RELEASES.md#version-1180-2017-06-08 |
| 22 | + |
| 23 | +### What's in 1.18.0 stable |
| 24 | + |
| 25 | +As usual, Rust 1.18.0 is a collection of improvements, cleanups, and new |
| 26 | +features. |
| 27 | + |
| 28 | +One of the largest changes is a long time coming: core team members Carol |
| 29 | +Nichols and Steve Klabnik have been writing a new edition of "The Rust |
| 30 | +Programming Language", the official book about Rust. It's being [written openly |
| 31 | +on GitHub](https://github.com/rust-lang/book), and has over a hundred |
| 32 | +contributors in total. This release [includes the first draft of the second |
| 33 | +edition in our online documentation](https://doc.rust-lang.org/stable/book/). |
| 34 | +19 out of 20 chapters have a draft; the draft of chapter 20 will land in Rust |
| 35 | +1.19. When the book is done, a print version will be made available through [No |
| 36 | +Starch Press](https://www.nostarch.com/Rust), if you'd like a paper copy. We're |
| 37 | +still working with the editors at No Starch to improve the text, but we wanted |
| 38 | +to start getting a wider audience now. |
| 39 | + |
| 40 | +The new edition is a complete re-write from the ground up, using the last two |
| 41 | +years of knowledge we've gained from teaching people Rust. You'll find |
| 42 | +brand-new explanations for a lot of Rust's core concepts, new projects to |
| 43 | +build, and all kinds of other good stuff. Please check it out and [let us know |
| 44 | +what you think](https://github.com/rust-lang/book/issues/new)! |
| 45 | + |
| 46 | +As for the language itself, an old feature has learned some new tricks: the |
| 47 | +`pub` keyword has been expanded a bit. Experienced Rustaceans will know that |
| 48 | +items are private by default in Rust, and you can use the `pub` keyword to make |
| 49 | +them public. In Rust 1.18.0, `pub` has [gained a new |
| 50 | +form](https://github.com/rust-lang/rust/pull/40556): |
| 51 | + |
| 52 | +```rust |
| 53 | +pub(crate) bar; |
| 54 | +``` |
| 55 | + |
| 56 | +The bit inside of `()` is a 'restriction', which refines the notion of how this |
| 57 | +is made public. Using the `crate` keyword like the example above means that |
| 58 | +`bar` would be public to the entire crate, but not outside of it. This makes it |
| 59 | +easier to declare APIs that are "public to your crate", but not exposed to your |
| 60 | +users. This was *possible* with the existing module system, but often very |
| 61 | +awkward. |
| 62 | + |
| 63 | +You can also specify a path, like this: |
| 64 | + |
| 65 | +```rust |
| 66 | +pub(in a::b::c) foo; |
| 67 | +``` |
| 68 | + |
| 69 | +This means "usable within the hierarchy of `a::b::c`, but not elsewhere." This |
| 70 | +feature was defined in [RFC |
| 71 | +1422](https://github.com/rust-lang/rfcs/blob/master/text/1422-pub-restricted.md) |
| 72 | +and [is documented in the |
| 73 | +reference](https://doc.rust-lang.org/stable/reference/visibility-and-privacy.html#pubin-path-pubcrate-pubsuper-and-pubself). |
| 74 | + |
| 75 | +For our Windows users, Rust 1.18.0 has [a new attribute, |
| 76 | +`#![windows_subsystem]`](https://github.com/rust-lang/rust/pull/40870). It |
| 77 | +works like this: |
| 78 | + |
| 79 | +```rust |
| 80 | +#![windows_subsystem(console)] |
| 81 | +#![windows_subsystem(windows)] |
| 82 | +``` |
| 83 | + |
| 84 | +These control the [`/SUBSYSTEM` flag](https://msdn.microsoft.com/en-us/library/fcc1zstk.aspx) |
| 85 | +in the linker. For now, only `console` and `windows` are supported. |
| 86 | + |
| 87 | +When is this useful? In the simplest terms, if you're developing a graphical |
| 88 | +application, and do not specify `windows`, a console window would flash up upon |
| 89 | +your application's start. With this flag, it won't. |
| 90 | + |
| 91 | +Finally, Rust's tuples, enum variant fields, and structs (without `#[repr]`) have |
| 92 | +always had an undefined layout. [We've turned on automatic re-ordering](https://github.com/rust-lang/rust/pull/40377), which can result in smaller sizes |
| 93 | +through reducing padding. Consider a struct like this: |
| 94 | + |
| 95 | +```rust |
| 96 | +struct Suboptimal(u8, u16, u8); |
| 97 | +``` |
| 98 | + |
| 99 | +In previous versions of Rust on the x86_64 platform, this struct would have the |
| 100 | +size of six bytes. But looking at the source, you'd expect it to have four. The |
| 101 | +extra two bytes come from padding; given that we have a `u16` here, it should be |
| 102 | +aligned to two bytes. But in this case, it's at offset one. To move it to offset |
| 103 | +two, another byte of padding is placed after the first `u8`. To give the whole struct |
| 104 | +a proper alignment, another byte is added after the second `u8` as well, giving us |
| 105 | +`1 + 1 (padding) + 2 + 1 + 1 (padding) = 6 bytes`. |
| 106 | + |
| 107 | +But what if our struct looked like this? |
| 108 | + |
| 109 | +```rust |
| 110 | +struct Optimal(u8, u8, u16); |
| 111 | +``` |
| 112 | + |
| 113 | +This struct is properly aligned; the `u16` lies on a two byte boundary, and so |
| 114 | +does the entire struct. No padding is needed. This gives us `1 + 1 + 2 = 4 |
| 115 | +bytes`. |
| 116 | + |
| 117 | +When designing Rust, we left the details of memory layout undefined for just |
| 118 | +this reason. Because we didn't commit to a particular layout, we can make |
| 119 | +improvements to it, such as in this case where the compiler can optimize |
| 120 | +`Suboptimal` into `Optimal` automatically. And if you check the sizes of |
| 121 | +`Suboptimal` and `Optimal` on Rust 1.18.0, you'll see that they both have a |
| 122 | +size of four bytes. |
| 123 | + |
| 124 | +We've been planning this change for a while; previous versions of Rust included |
| 125 | +this optimization on the nightly channel, but some people wrote unsafe code |
| 126 | +that assumed the exact details of the representation. We rolled it back while |
| 127 | +we fixed all instances of this that we know about, but if you find some code |
| 128 | +breaks due to this, please let us know so we can help fix it! |
| 129 | + |
| 130 | +We've been planning on moving `rustdoc` to use a CommonMark compliant markdown |
| 131 | +parser for a long time now. However, just switching over can introduce |
| 132 | +regressions where the CommonMark spec differs from our existing parser, |
| 133 | +Hoedown. As part of the transition plan, [a new flag has been added to |
| 134 | +`rustdoc`](https://github.com/rust-lang/rust/pull/40338), |
| 135 | +`--enable-commonmark`. This will use the new parser instead of the old one. |
| 136 | +Please give it a try! As far as we know, both parsers will produce identical |
| 137 | +results, but we'd be interested in knowing if you find a scenario where the |
| 138 | +rendered results differ! |
| 139 | + |
| 140 | +Finally, compiling `rustc` itself is now [15%-20% |
| 141 | +faster](https://github.com/rust-lang/rust/pull/41469). Each commit message in |
| 142 | +this PR goes over the details; there were some inefficiencies, and now they've |
| 143 | +been cleaned up. |
| 144 | + |
| 145 | +See the [detailed release notes][notes] for more. |
| 146 | + |
| 147 | +#### Library stabilizations |
| 148 | + |
| 149 | +Seven new APIs were stabilized this release: |
| 150 | + |
| 151 | +- [`Child::try_wait`] is a non-blocking form of `Child::wait`. |
| 152 | +- [`HashMap::retain`] and [`HashSet::retain`] bring the `retain` API `Vec<T>` has to these two hash data structures. |
| 153 | +- [`PeekMut::pop`] lets you pop the top element from a `BinaryHeap<T>` after you've already peeked at it without needing to reorder the heap a second time. |
| 154 | +- [`TcpStream::peek`], [`UdpSocket::peek`], [`UdpSocket::peek_from`] let you peek at a stream or socket. |
| 155 | + |
| 156 | +[`Child::try_wait`]: https://doc.rust-lang.org/std/process/struct.Child.html#method.try_wait |
| 157 | +[`HashMap::retain`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.retain |
| 158 | +[`HashSet::retain`]: https://doc.rust-lang.org/std/collections/struct.HashSet.html#method.retain |
| 159 | +[`PeekMut::pop`]: https://doc.rust-lang.org/std/collections/binary_heap/struct.PeekMut.html#method.pop |
| 160 | +[`TcpStream::peek`]: https://doc.rust-lang.org/std/net/struct.TcpStream.html#method.peek |
| 161 | +[`UdpSocket::peek_from`]: https://doc.rust-lang.org/std/net/struct.UdpSocket.html#method.peek_from |
| 162 | +[`UdpSocket::peek`]: https://doc.rust-lang.org/std/net/struct.UdpSocket.html#method.peek |
| 163 | + |
| 164 | +See the [detailed release notes][notes] for more. |
| 165 | + |
| 166 | +#### Cargo features |
| 167 | + |
| 168 | +Cargo has [added support](https://github.com/rust-lang/cargo/pull/3842) for the Pijul VCS, |
| 169 | +which is written in Rust. `cargo new my-awesome-project --vcs=pijul` will get you going! |
| 170 | + |
| 171 | +To supplement the `--all` flag, Cargo now has [several new |
| 172 | +flags](https://github.com/rust-lang/cargo/pull/3901) such as `--bins`, |
| 173 | +`--examples`, `--tests`, and `--benches`, which will let you build all programs of |
| 174 | +that type. |
| 175 | + |
| 176 | +Finally, Cargo now supports [Haiku](https://github.com/rust-lang/cargo/pull/3952) and |
| 177 | +[Android](https://github.com/rust-lang/cargo/pull/3885)! |
| 178 | + |
| 179 | +See the [detailed release notes][notes] for more. |
| 180 | + |
| 181 | +### Contributors to 1.18.0 |
| 182 | + |
| 183 | +Many people came together to create Rust 1.18. We couldn't have done it without |
| 184 | +all of you. [Thanks!](https://thanks.rust-lang.org/rust/1.18.0) |
0 commit comments