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


Rust IterMut.into_slice用法及代碼示例

本文簡要介紹rust語言中 std::slice::IterMut.into_slice 的用法。

用法

pub fn into_slice(self) -> &'a mut [T]

將基礎數據視為原始數據的子切片。

為避免創建 &mut 引用該別名,這將被強製使用迭代器。

例子

基本用法:

// First, we declare a type which has `iter_mut` method to get the `IterMut`
// struct (`&[usize]` here):
let mut slice = &mut [1, 2, 3];

{
    // Then, we get the iterator:
    let mut iter = slice.iter_mut();
    // We move to next element:
    iter.next();
    // So if we print what `into_slice` method returns here, we have "[2, 3]":
    println!("{:?}", iter.into_slice());
}

// Now let's modify a value of the slice:
{
    // First we get back the iterator:
    let mut iter = slice.iter_mut();
    // We change the value of the first element of the slice returned by the `next` method:
    *iter.next().unwrap() += 1;
}
// Now slice is "[2, 2, 3]":
println!("{:?}", slice);

相關用法


注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 std::slice::IterMut.into_slice。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。