How do I avoid having to clone()
twice? I am using clone()
to turn &String
into String
. Alternative solutions are welcome.
fn parse(mut lexer: Lexer) -> Vec<Expr> {
let mut expr_buffer = Vec::new();
let mut label = String::new();
while let Some(token) = lexer.next() {
match token.as_str() {
"$}" => break,
"$c" => {
let constants = parse_until(&mut lexer, "$.");
let constants = constants.iter()
.map(|constant| Expr::Constant(constant.clone())); // First `clone()`.
expr_buffer.extend(constants)
}
token => label = token.to_owned(),
}
}
return expr_buffer
}
fn parse_until(lexer: &mut Lexer, end: &str) -> Vec<String> {
let mut string_buffer = Vec::new();
while let Some(token) = &lexer.next() {
if token == end {
return string_buffer
}
string_buffer.push(token.clone()) // Second `clone()`.
}
panic!()
}