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


Rust Iter.as_slice用法及代碼示例

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

用法

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

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

這與原始切片具有相同的生命周期,因此迭代器可以在它存在時繼續使用。

例子

基本用法:

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

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

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

相關用法


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