/****************************************************************************** * This program demonstrates the Stack ADT * * Copyright © 2021 Richard Lesh. All rights reserved. *****************************************************************************/ mod utils; use std::env; use std::error; use std::sync::Arc; struct IntegerStack { // Internal representation for the stack is a list let mut stack:Vec = Vec::new(); } impl IntegerStack { fn new() -> IntegerStack { self.stack = Vec::new(); } fn pop() -> Result> { if self.stack.len() == 0 { return Err(Arc::new(utils::CustomError::RuntimeError("Can't pop on empty stack!".to_string()))); } return self.stack.pop().unwrap(); } fn push(value:isize) -> () { self.stack.push(value); } fn peek() -> Result> { if self.stack.len() == 0 { return Err(Arc::new(utils::CustomError::RuntimeError("Can't peek on empty stack!".to_string()))); } return self.stack[self.stack.len() - 1]; } } fn main() { let args: Vec = env::args().collect(); let mut stack:IntegerStack = IntegerStack::new(); { let mut x:isize = 1; while x <= 10 { stack.push(x); x += 1; } } match (|| -> Result<(), Arc>{ println!("{}", stack.peek()); { let mut x_:isize = 1; while x_ <= 11 { println!("{}", stack.pop()); x_ += 1; } } return Ok(()); })() { Ok(()) => {}, Err(ex) => { if let Some(ex) = utils::isCustomErrorType(ex.clone(), utils::CustomError::RuntimeError("".to_string())){ println!("{}", format!("{}", ex)); } } }; }