Pure Programmer
Blue Matrix


Cluster Map

Abstract Data Types (ADT)

L1

This page is under construction. Please come back later.

ADTStack.rs
/******************************************************************************
 * This program demonstrates the Stack ADT
 * 
 * Copyright © 2021 Richard Lesh.  All rights reserved.
 *****************************************************************************/

#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#[macro_use]
mod utils;

struct IntegerStack {
// Internal representation for the stack is a list
	stack:Vec<isize>,

}
impl IntegerStack {
	fn new() -> IntegerStack {
		IntegerStack {
			stack: Vec::new()
		}
	}
	fn pop(&mut self) -> Result<isize, utils::CustomError> {
		if self.stack.len() == 0 {
			return Err(utils::CustomError::RuntimeError("Can't pop on empty stack!".to_string()));
		}
		return Ok(self.stack.pop().unwrap());
	}
	fn push(&mut self, value:isize) -> () {
		self.stack.push(value);
	}
	fn peek(&mut self) -> Result<isize, utils::CustomError> {
		if self.stack.len() == 0 {
			return Err(utils::CustomError::RuntimeError("Can't peek on empty stack!".to_string()));
		}
		return Ok(self.stack[self.stack.len() - 1]);
	}
}

fn main() {
	let mut stack:IntegerStack = IntegerStack::new();
	{
		let mut x:isize = 1;
		while x <= 10 {
			stack.push(x);
			x += 1;
		}
	}
	match (|| -> Result<(), utils::CustomError>{
		println!("{}", stack.peek()?);
		{
			let mut _x:isize = 1;
			while _x <= 11 {
				println!("{}", stack.pop()?);
				_x += 1;
			}
		}
		return Ok(());
	})() {
		Ok(()) => {},
		Err(ex) => {
			match ex {
				utils::CustomError::RuntimeError(ex) => {
					println!("{}", ex);
				}
				_ => {
					println!("Unknown Exception!");
				}
			}
		}
	};
}

Output
$ rustc ADTStack.rs error: expected identifier, found keyword `let` --> ADTStack.rs:14:2 | 12 | struct IntegerStack { | ------------ while parsing this struct 13 | // Internal representation for the stack is a list 14 | let mut stack:Vec<isize> = Vec::new(); | ^^^ expected identifier, found keyword error[E0424]: expected value, found module `self` --> ADTStack.rs:19:3 | 18 | fn new() -> IntegerStack { | --- this function doesn't have a `self` parameter 19 | self.stack = Vec::new(); | ^^^^ `self` value is a keyword only available in methods with a `self` parameter | help: add a `self` receiver parameter to make the associated `fn` a method | 18 | fn new(&self) -> IntegerStack { | +++++ error[E0424]: expected value, found module `self` --> ADTStack.rs:22:6 | 21 | fn pop() -> Result<isize, Arc<dyn error::Error>> { | --- this function doesn't have a `self` parameter 22 | if self.stack.len() == 0 { | ^^^^ `self` value is a keyword only available in methods with a `self` parameter | help: add a `self` receiver parameter to make the associated `fn` a method | 21 | fn pop(&self) -> Result<isize, Arc<dyn error::Error>> { | +++++ error[E0424]: expected value, found module `self` --> ADTStack.rs:25:10 | 21 | fn pop() -> Result<isize, Arc<dyn error::Error>> { | --- this function doesn't have a `self` parameter ... 25 | return self.stack.pop().unwrap(); | ^^^^ `self` value is a keyword only available in methods with a `self` parameter | help: add a `self` receiver parameter to make the associated `fn` a method | 21 | fn pop(&self) -> Result<isize, Arc<dyn error::Error>> { | +++++ error[E0424]: expected value, found module `self` --> ADTStack.rs:28:3 | 27 | fn push(value:isize) -> () { | ---- this function doesn't have a `self` parameter 28 | self.stack.push(value); | ^^^^ `self` value is a keyword only available in methods with a `self` parameter | help: add a `self` receiver parameter to make the associated `fn` a method | 27 | fn push(&self, value:isize) -> () { | ++++++ error[E0424]: expected value, found module `self` --> ADTStack.rs:31:6 | 30 | fn peek() -> Result<isize, Arc<dyn error::Error>> { | ---- this function doesn't have a `self` parameter 31 | if self.stack.len() == 0 { | ^^^^ `self` value is a keyword only available in methods with a `self` parameter | help: add a `self` receiver parameter to make the associated `fn` a method | 30 | fn peek(&self) -> Result<isize, Arc<dyn error::Error>> { | +++++ error[E0424]: expected value, found module `self` --> ADTStack.rs:34:10 | 30 | fn peek() -> Result<isize, Arc<dyn error::Error>> { | ---- this function doesn't have a `self` parameter ... 34 | return self.stack[self.stack.len() - 1]; | ^^^^ `self` value is a keyword only available in methods with a `self` parameter | help: add a `self` receiver parameter to make the associated `fn` a method | 30 | fn peek(&self) -> Result<isize, Arc<dyn error::Error>> { | +++++ error[E0424]: expected value, found module `self` --> ADTStack.rs:34:21 | 30 | fn peek() -> Result<isize, Arc<dyn error::Error>> { | ---- this function doesn't have a `self` parameter ... 34 | return self.stack[self.stack.len() - 1]; | ^^^^ `self` value is a keyword only available in methods with a `self` parameter | help: add a `self` receiver parameter to make the associated `fn` a method | 30 | fn peek(&self) -> Result<isize, Arc<dyn error::Error>> { | +++++ error[E0308]: mismatched types --> ADTStack.rs:18:14 | 18 | fn new() -> IntegerStack { | --- ^^^^^^^^^^^^ expected `IntegerStack`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression error[E0599]: no method named `push` found for struct `IntegerStack` in the current scope --> ADTStack.rs:44:10 | 12 | struct IntegerStack { | ------------------- method `push` not found for this struct ... 44 | stack.push(x); | ------^^^^--- | | | | | this is an associated function, not a method | help: use associated function syntax instead: `IntegerStack::push(stack, x)` | = note: found the following associated functions; to be used as methods, functions must have a `self` parameter note: the candidate is defined in an impl for the type `IntegerStack` --> ADTStack.rs:27:2 | 27 | fn push(value:isize) -> () { | ^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0599]: no method named `peek` found for struct `IntegerStack` in the current scope --> ADTStack.rs:49:24 | 12 | struct IntegerStack { | ------------------- method `peek` not found for this struct ... 49 | println!("{}", stack.peek()); | ------^^^^-- | | | | | this is an associated function, not a method | help: use associated function syntax instead: `IntegerStack::peek()` | = note: found the following associated functions; to be used as methods, functions must have a `self` parameter note: the candidate is defined in an impl for the type `IntegerStack` --> ADTStack.rs:30:2 | 30 | fn peek() -> Result<isize, Arc<dyn error::Error>> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0599]: no method named `pop` found for struct `IntegerStack` in the current scope --> ADTStack.rs:53:26 | 12 | struct IntegerStack { | ------------------- method `pop` not found for this struct ... 53 | println!("{}", stack.pop()); | ------^^^-- | | | | | this is an associated function, not a method | help: use associated function syntax instead: `IntegerStack::pop()` | = note: found the following associated functions; to be used as methods, functions must have a `self` parameter note: the candidate is defined in an impl for the type `IntegerStack` --> ADTStack.rs:21:2 | 21 | fn pop() -> Result<isize, Arc<dyn error::Error>> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to 12 previous errors Some errors have detailed explanations: E0308, E0424, E0599. For more information about an error, try `rustc --explain E0308`.
HelloWorld.rs
/******************************************************************************
 * Simple first program.
 * 
 * Copyright © 2020 Richard Lesh.  All rights reserved.
 *****************************************************************************/

#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]

fn main() {
	println!("Hello, world!");
}

Output
$ rustc HelloWorld.rs $ ./HelloWorld Hello, world!
HelloWorld.rs
/******************************************************************************
 * Simple first program.
 * 
 * Copyright © 2020 Richard Lesh.  All rights reserved.
 *****************************************************************************/

#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]

fn main() {
	println!("Hello, world!");
}

Output
$ rustc HelloWorld.rs $ ./HelloWorld Hello, world!

Questions

Projects

More ★'s indicate higher difficulty level.

References