'How to skip the first item(s) of an iterator in Rust?
When iterating over arguments (for example) thats the most straightforward way to skip the first N elements?
eg:
use std::env;
fn main() {
for arg in env::args() {
println!("Argument: {}", arg);
}
}
I tried env::args()[1..]
but slicing isn't supported.
Whats the simplest way to skip the first arguments of an iterator?
Solution 1:[1]
Turns out the .skip()
method can be used, eg:
use std::env;
fn main() {
for arg in env::args().skip(1) {
println!("Argument: {}", arg);
}
}
Solution 2:[2]
You could also do something like
fn main() {
let args: Vec<String> = env::args().collect();
for x in &args[1..]
{
println!("{:?}", x);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ideasman42 |
Solution 2 | byronodmon |