内容简介:Rust 1.59.0 稳定版已正式发布,新版本最值得关注的特性是支持在代码中内联汇编 (Inline assembly),其他变化包括:引入解构式赋值、默认关闭增量编译,以及 Const 泛型参数支持设置默认值等。 内联汇编 (Inline ...
Rust 1.59.0 稳定版已正式发布,新版本最值得关注的特性是支持在代码中内联汇编 (Inline assembly),其他变化包括:引入解构式赋值、默认关闭增量编译,以及 Const 泛型参数支持设置默认值等。
内联汇编 (Inline assembly)
此项特性的使用场景主要是控制底层执行,或者访问特定的机器指令。
例如面向 x86-64 目标平台进行编译时,可以用以下的方式编写代码:
use std::arch::asm;
// Multiply x by 6 using shifts and adds
let mut x: u64 = 4;
unsafe {
asm!(
"mov {tmp}, {x}",
"shl {tmp}, 1",
"shl {x}, 2",
"add {x}, {tmp}",
x = inout(reg) x,
tmp = out(reg) _,
);
}
assert_eq!(x, 4 * 6);
可以看到,此处格式化字符串的语法与 Rust 格式化字符串中使用的相同,除了asm!,global_asm!宏也支持这种用法。
内联汇编中使用的汇编语言和指令取决于目标架构。目前 Rust 编译器支持以下架构的内联汇编:
- x86 & x86-64
- ARM
- AArch64
- RISC-V
解构式赋值 (Destructuring assignments)
通过此项特性,支持在赋值语句的左侧等式使用元组、切片和结构体模式 (struct patterns)。
let (a, b, c, d, e);
(a, b) = (1, 2);
[c, .., d, _] = [1, 2, 3, 4, 5];
Struct { e, .. } = Struct { e: 5, f: 3 };
assert_eq!([1, 2, 1, 4, 5], [a, b, c, d, e]);
不过要注意使用 +=运算符的赋值语句尚未支持解构式赋值。
Const 泛型参数支持设置默认值
支持为 Const 泛型参数设置默认值,如下:
struct ArrayStorage<T, const N: usize = 2> {
arr: [T; N],
}
impl<T> ArrayStorage<T> {
fn new(a: T, b: T) -> ArrayStorage<T> {
ArrayStorage {
arr: [a, b],
}
}
}
在此前的版本中,类型参数必须要放在所有 const 参数之前,目前此限制已放宽,支持将它们交替排列使用。
fn cartesian_product<
T, const N: usize,
U, const M: usize,
V, F
>(a: [T; N], b: [U; M], f: F) -> [[V; N]; M]
where
F: FnMut(&T, &U) -> V
{
// ...
}
默认关闭增量编译
Rust 1.59.0 版本已默认禁用增量编译功能(可通过环境变量显式地启用:RUSTC_FORCE_INCREMENTAL=1 ),此项变更降低了已知 bug #94124 的影响,该 bug 会导致在增量编译过程中引发反序列化错误和 panic。
开发团队表示,他们会在 1.60.0 版本修复这个 bug(六周内),届时如果没有出现意外问题,增量编译会重新设置为默认启用。
稳定的 API 列表
部分方法和 trait 实现已稳定化,点此查看详情。
下载地址:https://github.com/rust-lang/rust/releases/tag/1.59.0
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 智能合约基础语言(十):Solidity内联汇编
- 关于Linux进程切换switch_to宏的一个细节(认识内联汇编)
- iframe内联框架之巧妙跨域
- 提升go编译器内联程度
- 重学前端:块级元素与内联元素
- 内联第三方依赖到自己的包中
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Agile Web Application Development with Yii 1.1 and PHP5
Jeffrey Winesett / Packt Publishing / 2010-08-27
In order to understand the framework in the context of a real-world application, we need to build something that will more closely resemble the types of applications web developers actually have to bu......一起来看看 《Agile Web Application Development with Yii 1.1 and PHP5》 这本书的介绍吧!