集册 Rust 语言中文版 盒语法和模式

盒语法和模式

欢马劈雪     最近更新时间:2020-08-04 05:37:59

110

目前,唯一稳定可靠地方法就是通过 Box::new 方法来创建 Box。当然,它不可能在稳定的 Rust 来析构匹配模式下的 Box

不稳定的 box 关键字可以用来创建和析构 Box。相关的例子如下:

#![feature(box_syntax, box_patterns)]

fn main() {
let b = Some(box 5);
match b {
Some(box n) if n < 0 => {
println!("Box contains negative number {}", n);
},
Some(box n) if n >= 0 => {
println!("Box contains non-negative number {}", n);
},
None => {
println!("No box");
},
_ => unreachable!()
}
}

注意这个功能目前隐藏在 box_syntax(盒创建方法) 和 box_patterns (析构和匹配模型)方法,因为这个语法在未来仍可能会被更改。

返回指针

在很多计算机语言中都有指针,用户可以通过返回一个指针来避免返回较大数据结构的拷贝。比如:

struct BigStruct {
one: i32,
two: i32,
// etc
one_hundred: i32,
}

fn foo(x: Box<BigStruct>) -> Box<BigStruct> {
Box::new(*x)
}

fn main() {
let x = Box::new(BigStruct {
one: 1,
two: 2,
one_hundred: 100,
});

let y = foo(x);
}

这里面的想法就是通过返回一个盒,用户可以仅仅拷贝一个指针,从而避免 拷贝 BigStruct 中的上百个 int 数。

如下为 Rust 的反模式,相反,可以编写成下面的方式:

#![feature(box_syntax)]

struct BigStruct {
one: i32,
two: i32,
// etc
one_hundred: i32,
}

fn foo(x: Box<BigStruct>) -> BigStruct {
*x
}

fn main() {
let x = Box::new(BigStruct {
one: 1,
two: 2,
one_hundred: 100,
});

let y: Box<BigStruct> = box foo(x);
}
展开阅读全文