Rust 2015 各版本更新要点

本文将汇总Rust 1.0之后各版本在语法和部分库函数方面的更新,略过编译器和标准库的性能优化、工具链和文档的改进等内容。

2015-05-15 Rust 1.0

2015-06-25 Rust 1.1

  • New std::fs APIs.

2015-08-06 Rust 1.2

  • Rc<[T]> is fully usable.

2015-09-17 Rust 1.3

  • new Duration API and enhancements to Error and Hash/Hasher.
  • 'static variables may now be recursive.

2015-10-29 Rust 1.4

  • When reading ‘lines’ to treat both \n and \r\n as a valid line-ending.

2015-12-10 Rust 1.5

2016-01-21 Rust 1.6

  • The core library is stable, as are most of its APIs. Rust’s standard library is two-tiered: there’s a small core library, libcore, and the full standard library, libstd, that builds on top of it. libcore is completely platform agnostic, and requires only a handful of external symbols to be defined. Rust’s libstd builds on top of libcore, adding support for memory allocation, I/O, and concurrency. Applications using Rust in the embedded space, as well as those writing operating systems, often eschew libstd, using only libcore.
  • The `#[图片上传失败...(image-f4d162-1516015456023)].

2016-03-02 Rust 1.7

  • support for custom hash algorithms in the standard library’s HashMap<K, V> type.

2016-04-14 Rust 1.8

  • “operator equals” operators, such as += and -=, are now overloadable via coresponding traits.

  • struct with no fields can have curly braces:

    struct Foo; // works
    struct Bar { } // also works
    

2016-05-26 Rust 1.9

  • The #[deprecated] attribute when applied to an API will generate warnings when used. The warnings may be suppressed with#[allow(deprecated)].
  • stabilization of the std::panic module.

2016-07-07 Rust 1.10

2016-08-18 Rust 1.11

2016-09-29 Rust 1.12

2016-11-10 Rust 1.13

  • new operator, ?, for error handling, a solid ergonomic improvement to the old try! macro.
fn read_username_from_file() -> Result<String, io::Error> {
    let mut f = File::open("username.txt")?;
    let mut s = String::new();

    f.read_to_string(&mut s)?;

    Ok(s)
}
  • Macros can now be used in type position (RFC 873).

  • Attributes can be applied to statements (RFC 16).

2016-12-22 Rust 1.14

2017-02-02 Rust 1.15

2017-03-16 Rust 1.16

2017-04-27 Rust 1.17

2017-06-08 Rust 1.18

2017-07-20 Rust 1.19

2017-08-31 Rust 1.20

  • Define “associated constants” on traits, structs, and enums.

    struct Struct;
    
    impl Struct {
      // associated constant
      const ID: u32 = 0;
      
      // associated function
        fn foo() {
            println!("foo is called");
        }
    }
    
    fn main() {
        Struct::foo();
        println!("the ID of Struct is: {}", Struct::ID);
    }
    
  • allow messages in the unimplemented!() macro. ie. unimplemented!("Waiting for 1.21 to be stable")

  • Upgrade to Unicode 10.0.0

2017-10-12 Rust 1.21

(0..10).for_each(|i| println!("{}", i));

2017-11-22 Rust 1.22

2018-01-04 Rust 1.23

  • AsciiExt methods are now defined directly on u8, char, [u8], and str types, so you no longer need to import the trait.
  • The various std::sync::atomic types now implement From their non-atomic types.

2018-02-15 Rust 1.24

The following functions may now be used inside a constant expression, for example, to initialize a static:

  • Cell, RefCell, and UnsafeCell’s new functions
  • The new functions of the various Atomic integer types
  • {integer}::min_value and max_value
  • mem’s size_of and align_of
  • ptr::null and null_mut

2018-03-29 Rust 1.25

  • nested import groups

    use std::fs::File;
    use std::io::Read;
    use std::path::{Path, PathBuf};
    

    can be now be written as:

    use std::{fs::File, io::Read, path::{Path, PathBuf}};
    
  • [repr(align(x))] lets you set the alignment of your structs.

  • Allow | at the start of a match arm.e.g.

    enum Foo { A, B, C }
    match x {
        Foo::A | Foo::B => println!("AB"),
        Foo::C => println!("C"),
    }
    // better code alignment
    match x {
        | Foo::A
        | Foo::B => println!("AB"),
        | Foo::C => println!("C"),
    }
    

2018-05-11 Rust 1.26

  • impl Trait

    // before
    fn foo() -> Box<Fn(i32) -> i32> {
        Box::new(|x| x + 1)
    }
    
    // after
    fn foo() -> impl Fn(i32) -> i32 {
        |x| x + 1
    }
    
    fn foo() -> impl Iterator<Item = i32> {
        vec![1, 2, 3]
            .into_iter()
            .map(|x| x + 1)
            .filter(|x| x % 2 == 0)
    }
    
    // before
    fn foo<T: Trait>(x: T) {
    
    // after
    fn foo(x: impl Trait) {
    
  • Nicer match bindings

    fn hello(arg: &Option<String>) {
        match arg {
            Some(name) => println!("Hello {}!", name),
            None => println!("I don't know who you are."),
        }
    }
    
    fn hello(arg: &mut Option<String>) {
        match arg {
            Some(name) => name.push_str(", world"),
            None => (),
        }
    }
    
  • main can return a Result

    use std::fs::File;
    
    fn main() -> Result<(), std::io::Error> {
        let f = File::open("bar.txt")?;
    
        Ok(())
    }
    
  • Inclusive ranges with ..=

    fn takes_u8(x: u8) {
        // ...
    }
    
    fn main() {
        for i in 0..=255 {
            println!("i: {}", i);
            takes_u8(i);
        }
    }
    
  • Basic slice patterns

    let arr = [1, 2, 3];
    
    match arr {
        [1, _, _] => "starts with one",
        [a, b, c] => "starts with something else",
    }
    
    fn foo(s: &[u8]) {
        match s {
            [a, b] => (),
            [a, b, c] => (),
            _ => (),
        }
    }
    
  • 128 bit integers

    let x: i128 = 0;
    let y: u128 = 0;
    
  • stabilized fs::read_to_string

    use std::fs;
    use std::net::SocketAddr;
    
    let foo: SocketAddr = fs::read_to_string("address.txt")?.parse()?;
    
  • Closures now implement Copy and/or Clone if all captured variables implement either or both traits

  • Stablise '_. The underscore lifetime can be used anywhere where a lifetime can be elided.

  • A lot of operations are now available in a const context. E.g. You can now index into constant arrays, reference and dereference into constants, and use Tuple struct constructors.

  • Added hexadecimal formatting for integers with fmt::Debug e.g. assert!(format!("{:02x?}", b"Foo\0") == "[46, 6f, 6f, 00]")

2018-06-21 Rust 1.27

  • the std::arch, arch::x86 & arch::x86_64 modules which contain SIMD intrinsics, a new macro called `is_x86_feature_detected!

  • dyn Trait

    // old => new
    Box<Foo> => Box<dyn Foo>
    &Foo => &dyn Foo
    &mut Foo => &mut dyn Foo
    
  • The #[must_use] attribute can now also be used on functions as well as types.

    #[must_use]
    fn double(x: i32) -> i32 {
        2 * x
    }
    
    fn main() {
        double(4); // warning: unused return value of `double` which must be used
    
        let _ = double(4); // (no warning)
    }
    

2018-08-02 Rust 1.28

  • NonZero number types.
  • Unit test functions marked with the #[test] attribute can now return Result<(), E: Debug> in addition to ().

2018-09-18 Rust 1.29

Three APIs were stabilized:

  • Arc<T>::downcast
  • Rc<T>::downcast
  • Iterator::flatten

2018-10-25 Rust 1.30

  • Procedural Macros

    #[route(GET, "/")]
    fn index() {}
    
    #[proc_macro_attribute]
    pub fn route(attr: TokenStream, item: TokenStream) -> TokenStream { }
    
  • Module system improvements
    Wherever you see a path like a::b::c someplace other than a use statement, you can ask:

    • Is a the name of a crate? Then we're looking for b::c inside of it.
    • Is a the keyword crate? Then we're looking for b::c from the root of our crate.
    • Otherwise, we're looking for a::b::c from the current spot in the module hierarchy.
    // old
    let json = ::serde_json::from_str("...");
    
    // new
    let json = serde_json::from_str("...");
    
  • Raw Identifiers

    // define a local variable named `for`
    let r#for = true;
    
    // define a function named `for`
    fn r#for() {
        // ...
    }
    
    // call that function
    r#for();
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容