在 Rust 中动态创建任一方向的范围 [英] Dynamically create a range in either direction in Rust

查看:30
本文介绍了在 Rust 中动态创建任一方向的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Rust,最近进行了一项练习,我必须遍历可能朝任一方向发展的数字.我尝试了以下方法,结果出乎意料.

I am learning Rust and recently went through an exercise where I had to iterate through numbers that could go in either direction. I tried the below with unexpected results.

#[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);
}

输出是

all_x: [1, 2, 3]
all_y: []
points: []

经过一番谷歌搜索,我找到了一个 解释 和一些 answers 基本上相当于使用 (a..b).rev()根据需要.

After some googling I found an explanation and some old answers which basically amount to use (a..b).rev() as needed.

我的问题是,我如何以动态方式执行此操作?如果我像这样使用 if...else

My question is, how do I do this in a dynamic way? If I use an if...else like so

let all_x = if p1.x < p2.x { (p1.x..=p2.x) } else { (p2.x..=p1.x).rev() };

我收到类型错误,因为 elseif

I get a type error because the else is different than the 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<_>>`

在尝试了 let all_x: dyn Rangelet all_x: dyn Iterator 等一系列不同的变体之后,我设法做到这一点的唯一方法是将它们转换为集合,然后返回到迭代器.

After trying a bunch of different variations on let all_x: dyn Range<Item = i32>, let all_x: dyn Iterator<Item = i32>, etc, the only way I have managed to do this is by turning them into collections and then back to iterators.

let all_x: Vec<i32>;
if p1.x < p2.x { all_x = (p1.x..=p2.x).collect(); }
else { all_x = (p2.x..=p1.x).rev().collect(); }
let all_x = all_x.into_iter();
println!("all_x: {:?}", all_x.clone().collect::<Vec<i32>>());

let all_y: Vec<i32>;
if p1.y < p2.y { all_y = (p1.y..=p2.y).collect(); }
else { all_y = (p2.y..=p1.y).rev().collect(); }
let all_y = all_y.into_iter();
println!("all_y: {:?}", all_y.clone().collect::<Vec<i32>>());

提供期望的结果

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 }]

但有点重复,不优雅,我假设在大量使用时效率不高.有没有更好的方法来处理这种情况?

but is a bit repetitive, inelegant and I'm assuming not very efficient at large numbers. Is there a better way to handle this situation?

注意:很抱歉包含 Point 结构.我无法让我的示例与 x1x2 等一起使用.对于不同的帖子可能会有不同的问题,哈哈.

NOTE: Sorry for including the Point struct. I could not get my example to work with x1, x2, etc. Probably a different question for a different post lol.

推荐答案

你可以动态调度它.将它们包装成 Box 并返回一个动态对象,在本例中为 Iterator.例如:

You can dynamically dispatch it. Wrapping them into a Box and returning a dynamic object, an Iterator in this case. For example:

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))
    }
}

游乐场

这篇关于在 Rust 中动态创建任一方向的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆