< 返回版块

yuyi98 发表于 2019-03-14 22:24

Tags:array, iterator

fn main() { let a = [1.5f32; 50]; for i in &a { println!("{}", i); } }

error[E0277]: &[f32; 50] is not an iterator --> src\main.rs:3:11 | 3 | for i in &a { | ^^ &[f32; 50] is not an iterator | = help: the trait std::iter::Iterator is not implemented for &[f32; 50] = note: required by std::iter::IntoIterator::into_iter

如果数组个数修改为32及以下(如[1.5f32; 31])就没有问题,请解释一下!

评论区

写评论
gaxxx 2019-03-15 10:32

那是不是这么写也还行。。。

use std::iter::repeat

repeat(1.5f32).take(50)
Mike Tang 2019-03-15 00:13

因为 Rust 目前还的数组迭代器定义很奇葩,是手动实现的,只实现到了32个,因为还没有number泛型,没法自动实现。

具体可参阅:https://doc.rust-lang.org/std/primitive.array.html

这一片内容:

impl<'a, T> IntoIterator for &'a [T; 26]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 10]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 31]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 25]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 8]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 23]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 18]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 29]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 30]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 15]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 20]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 21]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 14]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 9]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 24]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 1]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 16]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 15]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 25]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 17]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 3]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 5]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 28]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 4]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 0]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 6]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 16]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 13]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 18]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 11]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 32]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 0]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 19]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 22]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 6]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 27]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 9]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 1]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 22]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 19]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 3]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 27]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 4]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 11]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 8]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 2]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 30]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 5]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 12]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 26]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 7]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 13]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 7]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 32]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 21]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 28]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 20]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 12]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 31]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 2]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 10]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 23]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 17]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 29]
[src]
[+]
impl<'a, T> IntoIterator for &'a [T; 14]
[src]
[+]
impl<'a, T> IntoIterator for &'a mut [T; 24]
[src]
1 共 2 条评论, 1 页