Abstract Data Types (ADT)
This page is under construction. Please come back later.
/****************************************************************************** * 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<isize> = Vec::new(); } impl IntegerStack { fn new() -> IntegerStack { self.stack = Vec::new(); } fn pop() -> Result<isize, Arc<dyn error::Error>> { 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<isize, Arc<dyn error::Error>> { 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<String> = 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<dyn error::Error>>{ 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)); } } }; }
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`.
/****************************************************************************** * Simple first program. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ fn main() { println!("Hello, world!"); }
Output
$ rustc HelloWorld.rs
$ ./HelloWorld
Hello, world!
/****************************************************************************** * Simple first program. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ fn main() { println!("Hello, world!"); }
Output
$ rustc HelloWorld.rs
$ ./HelloWorld
Hello, world!
Questions
Projects
More ★'s indicate higher difficulty level.
References
- [[Rust Language Reference]]
- [[Rust Compiler]]