Joey O'Neill
Joey O'Neill

Reputation: 134

Why does my vector have 2 additional elements?

I'm taking input from stdin() and splitting it into a Vec<char>

For some reason there are 2 elements at the end of my vector. Can anyone tell me why they are there and how to get rid of them?

This is where I split the input into a Vec<char>:

// Splits regEx into vector of chars
let mut reg_ex: Vec<char> = input.chars().collect();

This is the code iterating through the Vec:

let mut i = 0;
for character in &reg_ex {
  println!("{} {}", i, character);
  i = i + 1;
}

And this is the output I get with 2 extra elements at the end:

User input required: 
aaa
0 a
1 a
2 a
3
4

Upvotes: 0

Views: 69

Answers (1)

orlp
orlp

Reputation: 117681

Assuming you are on Windows these are likely the newline characters \r\n.

You can get rid of them by calling .trim_end() on input. Note that this would also strip any whitespace at the end of your line, if you wanted to keep that.

Upvotes: 1

Related Questions