午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

Rust語言學(xué)習(xí)筆記(11)

 lichwoo 2021-07-10

作者:@zwvista
本文為作者原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處:https://www.cnblogs.com/zwvista/p/12758683.html


目錄

Rust 2015 vs Rust 2018
loop
impl Trait
dyn Trait
關(guān)聯(lián)函數(shù)和關(guān)聯(lián)常量
..=(閉區(qū)間)
Non-lexical lifetimes
? 操作符
Slice patterns

Rust 2015 vs Rust 2018

loop

loop 循環(huán)也能返回值

// old code
let x;
loop {
    x = 7;
    break;
}
// new code
let x = loop { break 7; };

impl Trait

表示參數(shù)以及返回值是特質(zhì)類型時(shí)使用 impl Trait 語法。

// Argument Position
trait Trait {}
fn foo<T: Trait>(arg: T) {
}
fn foo(arg: impl Trait) {
}
// Return Position
trait Trait {}
impl Trait for i32 {}
fn returns_a_trait_object() -> Box<dyn Trait> {
    Box::new(5)
}
fn returns_a_trait_object() -> impl Trait {
    5
}
// closures
fn returns_closure() -> Box<dyn Fn(i32) -> i32> {
    Box::new(|x| x + 1)
}
fn returns_closure() -> impl Fn(i32) -> i32 {
    |x| x + 1
}

dyn Trait

特質(zhì)類型的對(duì)象使用 dyn Trait 語法

trait Trait {}
impl Trait for i32 {}
// old
fn function1() -> Box<Trait> {
}
// new
fn function2() -> Box<dyn Trait> {
}
use std::rc::Rc;
trait Foo {}
impl Foo for i32 {
}
fn main() {
    let obj: Rc<dyn Foo> = Rc::new(5);
}

關(guān)聯(lián)函數(shù)和關(guān)聯(lián)常量

對(duì)應(yīng)于其他語言中的靜態(tài)方法和靜態(tài)常量

// 關(guān)聯(lián)函數(shù)
struct Struct;
impl Struct {
    fn foo() {
        println!("foo is an associated function of Struct");
    }
}
fn main() {
    Struct::foo();
}
// 結(jié)構(gòu)體的關(guān)聯(lián)常量
struct Struct;
impl Struct {
    const ID: u32 = 0;
}
fn main() {
    println!("the ID of Struct is: {}", Struct::ID);
}
// 特質(zhì)的關(guān)聯(lián)常量
trait Trait {
    const ID: u32;
}
struct Struct;
impl Trait for Struct {
    const ID: u32 = 5;
}
fn main() {
    println!("{}", Struct::ID);
}

..=(閉區(qū)間)

// 半開區(qū)間
// 1,2
for i in 1..3 {
    println!("i: {}", i);
}
// 閉區(qū)間
// 1,2,3
for i in 1..=3 {
    println!("i: {}", i);
}

Non-lexical lifetimes

fn main() {
    let mut x = 5;
    let y = &x;
    let z = &mut x; // now ok
    println!("y: {}", y); // error
}

? 操作符

// old
fn read_username_from_file() -> Result<String, io::Error> {
    let f = File::open("username.txt");
    let mut f = match f {
        Ok(file) => file,
        Err(e) => return Err(e),
    };
    let mut s = String::new();
    match f.read_to_string(&mut s) {
        Ok(_) => Ok(s),
        Err(e) => Err(e),
    }
}
// old
fn read_username_from_file() -> Result<String, io::Error> {
    let mut f = try!(File::open("username.txt"));
    let mut s = String::new();
    try!(f.read_to_string(&mut s));
    Ok(s)
}
// new
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)
}

Slice patterns

// 匹配片段
fn main() {
    greet(&[]);
    // output: Bummer, there's no one here :(
    greet(&["Alan"]);
    // output: Hey, there Alan! You seem to be alone.
    greet(&["Joan", "Hugh"]);
    // output: Hello, Joan and Hugh. Nice to see you are at least 2!
    greet(&["John", "Peter", "Stewart"]);
    // output: Hey everyone, we seem to be 3 here today.
}
fn greet(people: &[&str]) {
    match people {
        [] => println!("Bummer, there's no one here :("),
        [only_one] => println!("Hey, there {}! You seem to be alone.", only_one),
        [first, second] => println!(
            "Hello, {} and {}. Nice to see you are at least 2!",
            first, second
        ),
        _ => println!("Hey everyone, we seem to be {} here today.", people.len()),
    }
}
// 匹配片段
let arr = [1, 2, 3];
assert_eq!("ends with 3", match arr {
    [_, _, 3] => "ends with 3",
    [a, b, c] => "ends with something else",
});

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多