Rob*_*non 4 iterator range rust
我正在学习 Rust,最近进行了一次练习,我必须迭代可能朝任一方向发展的数字。我尝试了以下方法,得到了意想不到的结果。
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct Point {
x: i32,
y: i32
}
fn test() {
let p1 = Point { x: 1, y: 8 };
let p2 = Point { x: 3, y: 6 };
let all_x = p1.x..=p2.x;
println!("all_x: {:?}", all_x.clone().collect::<Vec<i32>>());
let all_y = p1.y..=p2.y;
println!("all_y: {:?}", all_y.clone().collect::<Vec<i32>>());
let points: Vec<Point> = all_x.zip(all_y).map(|(x, y)| Point { x, y }).collect();
println!("points: {:?}", points);
}
Run Code Online (Sandbox Code Playgroud)
输出是
all_x: [1, 2, 3]
all_y: []
points: []
Run Code Online (Sandbox Code Playgroud)
经过一番谷歌搜索后,我找到了一个解释和一些旧 答案,基本上可以根据(a..b).rev()
需要使用。
我的问题是,如何以动态方式做到这一点?如果我使用if...else
像这样
all_x: [1, 2, 3]
all_y: []
points: []
Run Code Online (Sandbox Code Playgroud)
我收到类型错误,因为else
与if
|
58 | let all_x = if p1.x < p2.x { (p1.x..=p2.x) }
| - ------------- expected because of this
| _________________|
| |
59 | | else { (p2.x..=p1.x).rev() };
| |____________^^^^^^^^^^^^^^^^^^^_- `if` and `else` have incompatible types
| |
| expected struct `RangeInclusive`, found struct `Rev`
|
= note: expected type `RangeInclusive<_>`
found struct `Rev<RangeInclusive<_>>`
Run Code Online (Sandbox Code Playgroud)
在尝试了一系列不同的 、 等变体之后let all_x: dyn Range<Item = i32>
,let all_x: dyn Iterator<Item = i32>
我设法做到这一点的唯一方法是将它们转换为集合,然后返回迭代器。
let all_x = if p1.x < p2.x { (p1.x..=p2.x) } else { (p2.x..=p1.x).rev() };
Run Code Online (Sandbox Code Playgroud)
这提供了期望的结果
all_x: [1, 2, 3]
all_y: [8, 7, 6]
points: [Point { x: 1, y: 8 }, Point { x: 2, y: 7 }, Point { x: 3, y: 6 }]
Run Code Online (Sandbox Code Playgroud)
但有点重复、不优雅,而且我认为在大量情况下效率不高。有没有更好的方法来处理这种情况?
注意:很抱歉包含该Point
结构。我无法让我的示例与x1
、x2
等一起使用。可能是不同帖子的不同问题,哈哈。
枚举itertools::Either
可以用来解决if/else语句中类型不兼容的错误。get_range_iter
使用如下所示的函数Either
可以减少代码重复。
use itertools::Either;
fn get_range_iter(start: i32, end: i32) -> impl Iterator<Item=i32> {
if start < end {
Either::Left(start..=end)
} else {
Either::Right((end..=start).rev())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct Point {
x: i32,
y: i32
}
fn main() {
let p1 = Point { x: 1, y: 8 };
let p2 = Point { x: 3, y: 6 };
let all_x = get_range_iter(p1.x, p2.x);
let all_y = get_range_iter(p1.y, p2.y);
println!("all_x: {:?}", all_x.collect::<Vec<_>>());
println!("all_y: {:?}", all_y.collect::<Vec<_>>());
}
Run Code Online (Sandbox Code Playgroud)
您可以动态调度它。将它们包装到 a 中Box
并返回一个动态对象,Iterator
在本例中为 an 。例如:
fn maybe_reverse_range(init: usize, end: usize, reverse: bool) -> Box<dyn Iterator<Item=usize>> {
if reverse {
Box::new((init..end).rev())
} else {
Box::new((init..end))
}
}
Run Code Online (Sandbox Code Playgroud)