官术网_书友最值得收藏!

Understanding the different editions of Rust

On December 6, 2018, a very important version of the Rust language, its compiler, and its standard library was released: stable version 1.31. This version has been defined as the 2018 edition, meaning it is a milestone that will be used as a reference for the years to come.

Before this, there was another version, 1.0, which was defined as the 2015 edition. This edition was characterized by the word stability. Up until version 1.0, every version of the compiler applied breaking changes to the language or to the standard library, forcing the developers to apply sweeping changes to their code base. From version 1.0, efforts have been made to ensure that any future version of the compiler can correctly compile any code written for version 1.0 or successive versions. This is called backward compatibility.

However, many features were applied to the language and to the standard library before the release of the 2018 edition. Many new libraries used these new features, meaning that these libraries could not be used by older compilers. For this reason, there was a need to tag a specific version of Rust as aimed at being used with newer libraries. This was the main reason for the 2018 edition.

Some of the features added to the language are marked as for the 2015 edition, while others are marked as for the 2018 edition. The features for the 2015 edition are just small improvements, while the features for the 2018 edition are more in-depth changes. Developers must mark their crates as for the 2018 edition in order to use the features that are specific to the 2018 edition.

In addition, although the 2015 edition marked a stable milestone for the language and the standard library, the command-line tools were not actually stabilized; they were still quite immature. In the three and a half years from May 2015 to December 2018, the main official command-line tools have matured, and the language has also been improved to allow more efficient coding. The 2018 edition can be characterized by the word productivity.

The following table shows a timeline of the features stabilized in the language, the standard library, and the tooling:

Many improvements have been applied since the 2015 edition. More information can be found in the official documentation (https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html). The most important improvements are listed as follows:

  • A new official tutorial book, available free online (https://doc.rust-lang.org/book/), or printed on paper (The Rust Programming Language by Steve Klabnik and Carol Nichols).
  • A revamped official website.
  • The formation of four domain working groups, which are open committees to design the future of the ecosystem in four key areas:
    • Networking: Designing the new asynchronous paradigm around a concept of delayed computation, named future, as it is already done in other languages, such as C++, C#, and JavaScript (with promises).
    • Command-line applications: Designing some standard libraries to support any non-graphical, non-embedded applications.
    • WebAssembly: Designing tools and libraries to build applications to be run inside web browsers.
    • Embedded software: Designing tools and libraries to build applications to be run on bare-metal systems or on strictly constrained hardware.
  • We witnessed some good improvements to the language:
    • Non-lexical lifetimes; any bindings that are no longer used are considered dead. For example, now this program is allowed:
fn main() {
let mut _a = 7;
let _ref_to_a = &_a;
_a = 9;
}
In this code, the object bound to the variable _a is borrowed by the variable _ref_to_a in the second statement. Prior to the introduction of non-lexical lifetimes, such bindings would last till the end of the scope, and so the last statement would have been illegal because it tries to change that object through binding _a when it is still borrowed to variable _ref_to_a. Now, because variable  _ref_to_a is no longer used, its lifetime ceases in the same line it is declared, and so, in the last statement, variable _a is again free to change its own object.
    • The Impl Trait feature, which allows functions to return unspecified types, such as closures.
    • The i128 and u128 native types.
    • Some other reserved keywords such as tryasync, and await.
    • The ? operator, usable even in the main function, because now it can return Result. The following program is an example of the main function returning a Result:
fn main() -> Result<(), String> {
Err("Hi".to_string())
}

It can succeed, by returning the usual empty tuple or fail by returning the type you specify. In this case, it was String. The following program is an example using the ? operator used in the main function:

fn main() -> Result<(), usize> {
let array = [12, 19, 27];
let found = array.binary_search(&19)?;
println!("Found {}", found);
let found = array.binary_search(&20)?;
println!("Found {}", found);
Ok(())
}

This program will print Found 1 on the standard output stream, meaning that the number 19 has been found at position 1, and it will print Error: 2 on the standard error stream, meaning that the number 20 hasn't been found, but that it should be inserted at position 2.

    • Procedural macros, which allow a kind of meta-programming, manipulating source code to generate Rust code at compile time. 
    • More powerful and more ergonomic pattern matching in match expressions.
  • And also some improvements to the standard tooling:
    • The rustup program, which allows users to easily choose the default compiler target or to update the toolchain.
    • The rustfix program, which converts a 2015 edition project to a 2018 edition project.
    • The Clippy program, which checks for non-idiomatic syntax, and suggests changes to code for better maintainability.
    • Faster compilation speed, in particular, if just a syntax check is required.
    • The Rust Language Server (RLS) program, which is currently still unstable, but which allows IDEs and programmable editors to spot syntax errors, and to suggest allowed operations.

Rust is still evolving as a language, like any other programming language. The following areas are still left to be improved:

  • The IDE tools, including a language interpreter (REPL) and a graphical debugger
  • Libraries and tools to support bare-metal and real-time software development
  • Application-level frameworks and libraries for the main application areas

This book will focus primarily on the third point on this list.

主站蜘蛛池模板: 朔州市| 华宁县| 法库县| 大港区| 临城县| 湛江市| 亚东县| 墨脱县| 黄冈市| 郯城县| 中山市| 谢通门县| 珠海市| 东乡县| 芒康县| 六枝特区| 上蔡县| 霍邱县| 澜沧| 班玛县| 恩施市| 司法| 通山县| 鄯善县| 石首市| 荔浦县| 柞水县| 新平| 黄山市| 丰顺县| 武强县| 饶平县| 聊城市| 昌都县| 高密市| 咸丰县| 秦皇岛市| 唐山市| 抚远县| 清徐县| 浪卡子县|