From a20cff8fd8098d8571348e19de0bc604c504a58e Mon Sep 17 00:00:00 2001 From: Peter Tseng Date: Mon, 17 Dec 2018 02:06:18 +0000 Subject: [PATCH 1/2] forth: Don't `use` Term I prefer to make it easier to see where `term` is being used. Also, this causes an error in Rust 2018: error[E0658]: imports can only refer to extern crate names passed with `--extern` on stable channel (see issue #53130) --> src/lib.rs:5:5 | 5 | use Term::*; | ^^^^ Of course there are probably other ways to fix that, but not something I want to do right now. --- exercises/forth/example.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/exercises/forth/example.rs b/exercises/forth/example.rs index 3a6820a27..616a57510 100644 --- a/exercises/forth/example.rs +++ b/exercises/forth/example.rs @@ -2,8 +2,6 @@ use std::collections::HashMap; use std::collections::LinkedList; use std::str::FromStr; -use Term::*; - pub type Value = i32; pub type ForthResult = Result<(), Error>; type StackResult = Result; @@ -39,11 +37,11 @@ impl FromStr for Term { fn from_str(s: &str) -> Result { match s { - ":" => Ok(StartDefinition), - ";" => Ok(EndDefinition), + ":" => Ok(Term::StartDefinition), + ";" => Ok(Term::EndDefinition), _ => Err(()), - }.or_else(|_| Value::from_str(s).map(Number)) - .or_else(|_| Ok(Word(s.to_ascii_lowercase()))) + }.or_else(|_| Value::from_str(s).map(Term::Number)) + .or_else(|_| Ok(Term::Word(s.to_ascii_lowercase()))) } } @@ -76,10 +74,10 @@ impl Forth { fn step_term(&mut self, term: Term) -> ForthResult { match term { - Number(value) => self.push(value), - Word(word) => self.step_word(&word), - StartDefinition => self.store_definition(), - EndDefinition => Err(Error::InvalidWord), + Term::Number(value) => self.push(value), + Term::Word(word) => self.step_word(&word), + Term::StartDefinition => self.store_definition(), + Term::EndDefinition => Err(Error::InvalidWord), } } @@ -113,13 +111,13 @@ impl Forth { loop { match self.code.pop_front() { - Some(EndDefinition) => break, + Some(Term::EndDefinition) => break, Some(term) => def.push_back(term), None => return Err(Error::InvalidWord), } } - if let Some(Word(name)) = def.pop_front() { + if let Some(Term::Word(name)) = def.pop_front() { self.store_word(name, def) } else { Err(Error::InvalidWord) From 093e9283438634f112bf7951ccf55e1c819cb43e Mon Sep 17 00:00:00 2001 From: Peter Tseng Date: Mon, 17 Dec 2018 02:08:55 +0000 Subject: [PATCH 2/2] forth: Use `?` not `try!` Would cause an error in Rust 2018: error: expected expression, found reserved keyword `try` --> src/lib.rs:72:13 | 72 | try!(self.step_term(term)) | ^^^ expected expression --- exercises/forth/example.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/forth/example.rs b/exercises/forth/example.rs index 616a57510..b32e57aa9 100644 --- a/exercises/forth/example.rs +++ b/exercises/forth/example.rs @@ -66,7 +66,7 @@ impl Forth { fn run(&mut self) -> ForthResult { while let Some(term) = self.code.pop_front() { - try!(self.step_term(term)) + self.step_term(term)? } Forth::ok()