Why chars().nth(index).unwrap() is so slow?

82 points QuiiBz

Hello, I'm facing an issue that I don't really understand. I started Rust a few weeks ago, so I don't have a huge experience with it.

I'm trying to do a simple lexer, and my eat function calls self.source.chars().nth(self.index).unwrap(); to get the next char to process. source is a &str. I'm benchmarking with a source length of 70 000 chars, and this part of the code takes a few seconds to complete. If I comment the nth(index).unwrap() part, the code runs in few us.

Can someone explain to me why is it this slow, and suggest any improvement? Thanks for any help.