首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >预期std::iter::Iterator,但std::iter::Iterator

预期std::iter::Iterator,但std::iter::Iterator
EN

Stack Overflow用户
提问于 2020-01-01 10:13:01
回答 2查看 511关注 0票数 2

我想表达以下几点:

给定一个矩阵和两个索引增量,从该矩阵返回所有四重数:沿行、列或对角线的四重数列。

代码语言:javascript
复制
use std::iter::Iterator;
use std::iter::Peekable;
use std::ops::Range;

struct Quads<'a> {
   mx: &'a Vec<Vec<u32>>,
   xs: &'a mut Peekable<Range<i32>>,
   ys: &'a mut Peekable<Range<i32>>,
   dx: i32,
   dy: i32,
}

impl<'a> Quads<'a> {
   fn new(mx: &'a Vec<Vec<u32>>, dx: i32, dy: i32) -> Quads<'a> {
      let ys = (if dy < 0 { -3 * dy } else { 0 })..(mx.len() as i32 - if dy > 0 { 4 * dy } else { 0 });
      let xs = 0..0;

      Quads{
         mx: mx,
         xs: &mut xs.peekable(),
         ys: &mut ys.peekable(),
         dx: dx,
         dy: dy,
      }
   }
}

impl<'a> Iterator for Quads<'a> {
   type Item = &'a mut dyn Iterator<Item = u32>;

   fn next(&mut self) -> Option<Self::Item> {
      while self.xs.peek() == None && self.ys.peek() != None {
         self.xs = &mut ((if self.dx < 0 { -3 * self.dx } else { 0 })..
                         (self.mx[0].len() as i32 - if self.dx > 0 { 4 * self.dx } else { 0 }))
                         .peekable();
         self.ys.next();
      }

      let y = self.ys.peek();
      if y == None {
         return None;
      }

      let y = *y.unwrap();
      let x = self.xs.next().unwrap();

      Some(&mut ((x..).step_by(self.dx as usize)
                      .zip((y..).step_by(self.dy as usize))
                     .take(4)
                     .map(|(x,y)| self.mx[y as usize][x as usize])))
   }
}

这会产生令人困惑的错误消息:

代码语言:javascript
复制
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
  --> src/main.rs:52:27
   |
52 |                      .map(|(x,y)| self.mx[y as usize][x as usize])))
   |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 33:4...
  --> src/main.rs:33:4
   |
33 | /    fn next(&mut self) -> Option<Self::Item> {
34 | |       while self.xs.peek() == None && self.ys.peek() != None {
35 | |          self.xs = &mut ((if self.dx < 0 { -3 * self.dx } else { 0 })..
36 | |                          (self.mx[0].len() as i32 - if self.dx > 0 { 4 * self.dx } else { 0 }))
...  |
52 | |                      .map(|(x,y)| self.mx[y as usize][x as usize])))
53 | |    }
   | |____^
   = note: ...so that the types are compatible:
           expected &&mut Quads<'a>
              found &&mut Quads<'a>
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 30:6...
  --> src/main.rs:30:6
   |
30 | impl<'a> Iterator for Quads<'a> {
   |      ^^
   = note: ...so that the types are compatible:
           expected std::iter::Iterator
              found std::iter::Iterator

它似乎表明它找到了与它所寻找的一样的东西。那怎么了?

打算使用

看看https://projecteuler.net/problem=11

当然,这个问题可以用更直截了当的方法来解决,但我正在学习如何用铁锈来表达复杂的事物。在这里,我试图表达一个Quad,它是一个Iterator,它可以从欧拉问题中提取四倍的数字,其中每个四倍就是一个Iterator本身。

Quad中的所有内容都表示Iterator的状态。xsys表示“当前单元格”坐标的迭代器,从它们开始下一个四倍。然后,next试图查看是否到达行的末尾,并通过将xs重新初始化为一个新的Iterator来推进到下一行。当ys到达最后一行之后,我们提取了所有的四倍。

然后是这样的:

代码语言:javascript
复制
for q in Quad::new(mx, 1, 0) {  ... process all quadruples along the rows }
for q in Quad::new(mx, 0, 1) {  ... process all quadruples along the columns }
for q in Quad::new(mx, 1, 1) {  ... process all quadruples along one diagonal }
for q in Quad::new(mx, 1, -1) {  ... process all quadruples along the other diagonal }

我想我已经捕捉到了这个想法,但我不知道编译器不喜欢它的什么地方,也不知道如何继续前进。

EN

回答 2

Stack Overflow用户

发布于 2020-01-03 10:12:13

好吧,所以我想出来了。rustc产生这样一条令人困惑的错误信息确实没有帮助--显然,它找到了它正在寻找的东西,但仍然不满意。

发布的代码有几个问题。我最初的假设是,通过将引用标记为可变的,我可以告诉编译器,无论谁接收引用,从今以后都要负责它,包括内存管理。虽然在某些情况下这可能是真的(我不确定;这还有待解决),但它肯定不适用于struct字段(Quadxs)和返回值。

代码语言:javascript
复制
struct Quads<'a> {
   mx: &'a Vec<Vec<u32>>,
   xs: Peekable<Range<i32>>,
   ys: Peekable<Range<i32>>,
   dx: i32,
   dy: i32,
}

另一个问题是,如果它不是引用,那么如何限制值的生存期。(例如在这种情况下,next返回的next只有效于mx)

另一个问题是表达式问题:如何使next返回一个Iterator (我不想泄露什么类型的Iterator),从而使编译器感到高兴。(如只是dyn Iterator做不到--“编译时不知道大小”)。这两个问题都是通过使用Box来解决的,它也可以用生命周期进行注释:

代码语言:javascript
复制
impl<'a> Iterator for Quads<'a> {
   type Item = Box<dyn Iterator<Item = u32> + 'a>;

   fn next(&mut self) -> Option<Self::Item> {
      ...
   }
}

另一个问题是,即使mx的使用是只读的,闭包|(x, y)| self.mx[y][x]仍然捕获self,这是一个可变的引用。这很简单:获取一个局部变量,然后是move

代码语言:javascript
复制
  let mx = self.mx;
  Some(Box::new(...
          .map(move |(x, y)| mx[y as usize][x as usize])))

差点忘了。非常奇怪的是,即使在我最初输入它的时候,它看起来也很可疑:step_by使用usize,它是无符号的,并不是真正构建一个通过添加给定的增量来枚举值的Range;而是构造一个跳过给定数量的元素的Iterator (差不多)。因此,需要一个元组迭代器:

代码语言:javascript
复制
struct Tup<T> {
   x: (T, T),
   d: (T, T),
}
...
impl<T: AddAssign + Copy> Iterator for Tup<T> {
   type Item = (T, T);
   fn next(&mut self) -> Option<(T, T)> {
      self.x.0 += self.d.0;
      self.x.1 += self.d.1;
      Some(self.x)
   }
}

因此,与其用Iterator压缩两个step_by,不如使用已知的初始值和增量初始化Tup

代码语言:javascript
复制
  Some(Box::new(Tup::new((x, y), (self.dx, self.dy))
                     .take(4)
                     .map(move |(x, y)| mx[y as usize][x as usize])))
票数 2
EN

Stack Overflow用户

发布于 2020-01-03 11:57:23

另一种解决方案不是导出迭代器,而是传递要在其中运行的操作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59551596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档