From d02e147295518b32f744a5906f8468cc6f2551bf Mon Sep 17 00:00:00 2001 From: stratact <stratact1@gmail.com> Date: Mon, 28 May 2018 04:06:07 -0700 Subject: [PATCH] Use iterators to join strings rather than join from a collected vec allocation --- src/lib/parser/statement/case.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lib/parser/statement/case.rs b/src/lib/parser/statement/case.rs index 75732370..10c37c33 100644 --- a/src/lib/parser/statement/case.rs +++ b/src/lib/parser/statement/case.rs @@ -36,7 +36,13 @@ pub(crate) fn parse_case<'a>( binding = Some(splitter.next().ok_or(CaseError::NoBindVariable)?); match splitter.next() { Some("if") => { - let string = splitter.collect::<Vec<_>>().join(" "); + // Joining by folding is more efficient than collecting into Vec and then joining + let mut string = splitter.fold(String::with_capacity(5), |mut state, element| { + state.push_str(element); + state.push(' '); + state + }); + string.pop(); // Pop out the unneeded ' ' character if string.is_empty() { return Err(CaseError::NoConditional); } @@ -47,7 +53,13 @@ pub(crate) fn parse_case<'a>( } } Some("if") => { - let string = splitter.collect::<Vec<_>>().join(" "); + // Joining by folding is more efficient than collecting into Vec and then joining + let mut string = splitter.fold(String::with_capacity(5), |mut state, element| { + state.push_str(element); + state.push(' '); + state + }); + string.pop(); // Pop out the unneeded ' ' character if string.is_empty() { return Err(CaseError::NoConditional); } -- GitLab