當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。