当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Rust IterMut.as_slice用法及代码示例


本文简要介绍rust语言中 alloc::slice::IterMut.as_slice 的用法。

用法

pub fn as_slice(&self) -> &[T]

将基础数据视为原始数据的子切片。

为避免创建 &mut [T] 引用该别名,返回的切片从应用该方法的迭代器借用其生命周期。

例子

基本用法:

let mut slice: &mut [usize] = &mut [1, 2, 3];

// First, we get the iterator:
let mut iter = slice.iter_mut();
// So if we check what the `as_slice` method returns here, we have "[1, 2, 3]":
assert_eq!(iter.as_slice(), &[1, 2, 3]);

// Next, we move to the second element of the slice:
iter.next();
// Now `as_slice` returns "[2, 3]":
assert_eq!(iter.as_slice(), &[2, 3]);

相关用法


注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 alloc::slice::IterMut.as_slice。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。