在Rust切片中可以使用”step”吗?

huangapple go评论77阅读模式
英文:

Can we use step in Rust slice?

问题

在Python中,我们可以获取奇数索引的子数组,如下所示:

odd = array[1::2]

在Rust中,我们可以使用简单的语法或任何特性来实现吗?像这样:

let vec = vec![1; 10];
let sli = &vec[1..10:2];

上述代码无法通过编译。

英文:

In python, we can get subarray in odd indexes like:

odd = array[1::2]

Can we do this in Rust, using simple syntax or any traits? like:

let vec = vec![1; 10];
let sli = &vec[0.2.10];

The above code cannot pass the compile.

答案1

得分: 6

不,一个slice

连续序列的动态大小视图

不过,你可以将原始切片的引用收集到一个Vec中:

let odd: Vec<&i32> = array.iter().step_by(2).collect();

但对于许多应用程序,你甚至不需要完全collect,可以直接使用Iterator

英文:

No, a slice is
> A dynamically-sized view into a contiguous sequence

You could collect references into the original slice into a Vec though:

let odd: Vec&lt;&amp;i32&gt; = array.iter().step_by(2).collect();

But for many applications you don't even need to collect at all but can use the Iterator directly.

答案2

得分: 6

Rust没有这个Python特性。但是,您可以使用.step_by()方法对任何迭代器应用步长。

let v: Vec<i32> = (0..10).step_by(2).collect();
assert_eq!(v, vec![0, 2, 4, 6, 8]);

但是,请注意步长必须是正数,不像Python,其中负步长意味着倒着遍历列表。要在Rust中反转迭代器,通常可以使用.rev()方法。

let v2: Vec<i32> = v.into_iter().rev().collect();
assert_eq!(v2, vec![8, 6, 4, 2, 0]);

如果您正在使用张量库,如ndarray,它将有自己的方式来使用步长进行切片,类似于这个ndarray宏

英文:

Rust doesn't have this Python feature. However, you can apply a step size to any iterator by using the .step_by() method.

let v: Vec&lt;i32&gt; = (0..10).step_by(2).collect();
assert_eq!(v, vec![0, 2, 4, 6, 8]);

However, note that the step size must be positive, unlike Python, where a negative step size means to walk the list backwards. To reverse an iterator in Rust, you can usually use .rev().

let v2: Vec&lt;i32&gt; = v.into_iter().rev().collect();
assert_eq!(v2, vec![8, 6, 4, 2, 0]);

If you're using a tensor library, like ndarray, it'll have its own way of slicing with a step size, like this ndarray macro.

答案3

得分: 1

Rust切片语法不支持。

您可以通过使用.iter()方法与.enumerate().filter()方法来创建一个迭代器,仅产生奇数索引处的元素。如果您需要结果是一个Vec,那么您可以将这个迭代器收集到一个Vec中,

let vec = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let odd: Vec<_> = vec.iter()
                     .enumerate()
                     .filter(|&(i, _)| i % 2 != 0)
                     .map(|(_, e)| e)
                     .collect();
println!("{:?}", odd);

请注意,索引是从0开始的,因此这会给您原始向量的偶数元素的奇数索引处的元素。如果您想要偶数索引处的元素,您可以将i % 2 != 0更改为i % 2 == 0

英文:

No,Rust slicing syntax does not support.

you can achieve it by using the .iter() method with the .enumerate() and .filter() methods to create an iterator that only yields elements at odd indices. If you need the result to be a Vec, you can then collect this iterator into a Vec,

let vec = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let odd: Vec&lt;_&gt; = vec.iter()
                     .enumerate()
                     .filter(|&amp;(i, _)| i % 2 != 0)
                     .map(|(_, e)| e)
                     .collect();
println!(&quot;{:?}&quot;, odd);

Note that the indices are 0-based, so this gives you elements at odd indices, which are the even elements of the original vector. If you want the elements at even indices, you can change i % 2 != 0 to i % 2 == 0.

huangapple
  • 本文由 发表于 2023年7月28日 04:47:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76783321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定