diff --git a/exercises/forth/example.rs b/exercises/forth/example.rs index 006c6c1b9..24d4c0529 100644 --- a/exercises/forth/example.rs +++ b/exercises/forth/example.rs @@ -1,13 +1,13 @@ use std::collections::HashMap; -use std::collections::LinkedList; +use std::collections::VecDeque; use std::str::FromStr; pub type Value = i32; pub type ForthResult = Result<(), Error>; type StackResult = Result; -type Stack = LinkedList; -type Code = LinkedList; +type Stack = Vec; +type Code = VecDeque; type Definitions = HashMap; pub struct Forth { @@ -49,14 +49,14 @@ impl FromStr for Term { impl Forth { pub fn new() -> Forth { Forth { - code: LinkedList::new(), + code: Code::new(), defs: HashMap::new(), - stack: LinkedList::new(), + stack: Stack::new(), } } - pub fn stack(&self) -> Vec { - self.stack.iter().cloned().collect() + pub fn stack(&self) -> &[Value] { + self.stack.as_slice() } pub fn eval(&mut self, input: &str) -> ForthResult { @@ -125,7 +125,7 @@ impl Forth { } fn store_definition(&mut self) -> ForthResult { - let mut def = LinkedList::new(); + let mut def = Code::new(); loop { match self.code.pop_front() { @@ -143,12 +143,12 @@ impl Forth { } fn push(&mut self, value: Value) -> ForthResult { - self.stack.push_back(value); + self.stack.push(value); Forth::ok() } fn pop(&mut self) -> StackResult { - self.stack.pop_back().ok_or_else(|| { + self.stack.pop().ok_or_else(|| { eprintln!("Stack underflow"); Error::StackUnderflow }) @@ -189,7 +189,7 @@ impl Forth { Forth::ok() } - fn into_code(input: &str) -> LinkedList { + fn into_code(input: &str) -> Code { input .split(|c: char| c.is_whitespace() || c.is_control()) .map(Term::from_str)