Rust Syntax: Lookahead

The Rust Std Library is great, so my question is: Would Rust Peakable be ok to write an input buffer/ lookahead algorithm while precisely being able to write a lookahead algorithm for another language like SQL.

1)Does anyone have experience with Rust Peekable and are there any "do's and donts"?

  1. I have suedo code for the look ahead algorithm. Would it be better to write a lookahead algorithm from scratch or use Rust Peekable?
  2. Do you know of any online resources that demonstrate lookahead alogrithms in rust or other languages?

Thank you kindly

I'm not sure I understand what you are referring to, but maybe you are interested in peekable iterators from the standard library?

On (almost) any Rust iterator you can call the Iterator::peekable() method to get a new version of the iterator that supports seeing into the future (peeking) via the peek() and peek_mut() methods. The documentation for peekable() and peek() have some usage examples.

The implementation of the Peekable iterator is fairly straightforward: when you call peek(), it calls next() on the underlying iterator and internally stores the result before returning a reference to it. If you keep calling peek(), you will keep getting a reference to this cached result and the underlying iterator won't be advanced. When you finally call next(), the cached result is consumed and returned.

It is useful for when you want to have a look at the next value in an iterator to decide whether you are ready to deal with it or not, but you don't want to implement yourself the logic necessary to keep the value around in case you are not ready to use it.


There's buffer_redux's BufReader which can be used with a MinBuffered policy to essentially implement N bytes lookahead on a Read stream. I can't vouch for the crate's soundness, however, as it does use a lot of unsafe code internally.

Thank you kindly!

My question was not well worded. That's my error. Im writing a SQL implementation for a database. Im busy with the lexical analysis phase. In that phase the scanner needs to read ahead a few characters to correctly match multiple characters in a string. That can be done with the regex crate but im trying to find a simpler more concise way to do that with a look ahead algorithm.

So far I have this:

pub fn is_multi_character_pattern(mcp: &str) -> bool {
    matches!(mcp, "??(" | "??)")
fn match_multiple_characters(input: &str) -> bool {
    let pattern = "??)";

I do see the std::Chars library. I will look at that too.

What does one mean when they refer to a Cursor?

Thank you very much for your help.

Ok great. Thank you kindly.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.