内容简介:Rust 1.50.0 稳定版已发布,此版本改进了数组索引、扩展了对联合字段 (union field) 的安全访问,并将其添加到标准库。 具备 const-generic 特性的数组索引 此版本为任意长度const N的数组[T; N]增加了ops::Inde...
Rust 1.50.0 稳定版已发布,此版本改进了数组索引、扩展了对联合字段 (union field) 的安全访问,并将其添加到标准库。
具备 const-generic 特性的数组索引
此版本为任意长度const N
的数组[T; N]
增加了ops::Index
和IndexMut
实现,索引操作符[]
可通过内置的 compiler magic 在数组中运行。
fn second<C>(container: &C) -> &C::Output
where
C: std::ops::Index<usize> + ?Sized,
{
&container[1]
}
fn main() {
let array: [i32; 3] = [1, 2, 3];
assert_eq!(second(&array[..]), &2); // slices worked before
assert_eq!(second(&array), &2); // now it also works directly
}
数组中定义为const
类型的值支持重复
Rust 的数组可以写为列表[a, b, c]
或重复形式[x; N]
。
fn main() {
// This is not allowed, because `Option<Vec<i32>>` does not implement `Copy`.
let array: [Option<Vec<i32>>; 10] = [None; 10];
const NONE: Option<Vec<i32>> = None;
const EMPTY: Option<Vec<i32>> = Some(Vec::new());
// However, repeating a `const` value is allowed!
let nones = [NONE; 10];
let empties = [EMPTY; 10];
}
针对ManuallyDrop<T>
联合字段的安全分配
Rust 1.49 支持将ManuallyDrop<T>
字段添加到union
,从而允许合并Drop
。不过,当一个字段被赋值时,union
不会丢弃旧的值,因为它们不知道哪个变体此前是有效的,所以安全的 Rust 以前只限制为仅限于Copy
类型,而Copy
类型从来不会Drop
。当然,ManuallyDrop<T>
也不需要Drop
,因此 Rust 1.50 现在允许对这些字段进行安全分配。
Rust 1.50.0 还新增了 9 个稳定的函数:
bool::then
btree_map::Entry::or_insert_with_key
f32::clamp
f64::clamp
hash_map::Entry::or_insert_with_key
Ord::clamp
RefCell::take
slice::fill
UnsafeCell::get_mut
延伸阅读
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 算法-计算小数组在大数组中的索引
- python – 从一维数组的索引和值构造二维numpy数组
- JavaScript算法题:查找数字在数组中的索引
- LeetCode每日一题:找数组的中心索引(No.724)
- PHP重置数组为连续数字索引的几种方式总结
- Leetcode724:寻找数组的中心索引(java、python3)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Head First JavaScript Programming
Eric T. Freeman、Elisabeth Robson / O'Reilly Media / 2014-4-10 / USD 49.99
This brain-friendly guide teaches you everything from JavaScript language fundamentals to advanced topics, including objects, functions, and the browser’s document object model. You won’t just be read......一起来看看 《Head First JavaScript Programming》 这本书的介绍吧!