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


Rust Iter.as_slice用法及代码示例


本文简要介绍rust语言中 core::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大神的英文原创作品 core::slice::Iter.as_slice。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。