Vec<char> vs. String for a scanner: Which is more performant in Rust?

67 points Motor_Alternative944 71 comments

When writing an interpreter in Rust, does using Vec<char> or String for the scanner make a difference in terms of performance? Which one would be the better choice?

My Code:

pub struct Scanner<'a> {
    source: Vec<char>,
    tokens: Vec<Token<'a>>,
    start: usize,
    current: usize,
    line: i32,
}

I'm a hobbyist programmer, and I've always been very interested in compilers. I want to dive deeper into this area as a hobby, so I started with Crafting Interpreters. Along the way, I've also gained a much better understanding of Rust's borrow checker and some other concepts.

My goal with Rust is to design a very minimal, interpreted, type-safe programming language focused solely on the backend. I'm not sure if it's necessary for this, but I've also decided to start studying mathematics from scratch for the project.