I want to try to get familiar with Rust, so I'm attempting Crafting Interpreters. In the Longer Lexemes section it has a peek function that gets the character one ahead of the current index.
I'm not sure how to go about this in Rust. It seems that indexing strings is discouraged due to the design of the language and complexities with Unicode.
If I do chars.next() then it will consume the value. I thought that .as_bytes() on the original string could work, but that only supports ASCII values. From the internet, .chars().nth().unwrap() is the solution, but it seems to be very slow.
A practical example of @BallpointBen's comment about using
peekablewould be:Rust Playground Example
Output: