diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 82f3c173a..fc3113ecc 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -51,7 +51,7 @@ This file lists everyone, who contributed to this repo and wanted to show up her - Vincent Zalzal - Jonathan D B Van Schenck - James Goytia -- Sammy Plat +- Sammy Plat - Jonathan Dönszelmann - Ishaan Verma - Delphi1024 @@ -60,4 +60,5 @@ This file lists everyone, who contributed to this repo and wanted to show up her - Ridham177 - Hugo Salou - Dimitri Belopopsky -+ Henrik Abel Christensen +- Henrik Abel Christensen +- Peanutbutter_Warrior diff --git a/contents/stacks_and_queues/code/rust/Queue.rs b/contents/stacks_and_queues/code/rust/Queue.rs new file mode 100644 index 000000000..0f432e621 --- /dev/null +++ b/contents/stacks_and_queues/code/rust/Queue.rs @@ -0,0 +1,43 @@ +use std::collections::VecDeque; + +struct Queue { + list: VecDeque +} + +impl Queue { + fn new() -> Self { + Queue{ + list: VecDeque::new(), + } + } + + // Note that this returns a reference to the value + // This is in contrast to dequeue which gives ownership of the value + fn front(&self) -> Option<&T> { + self.list.front() + } + + fn dequeue(&mut self) -> Option { + self.list.pop_front() + } + + fn enqueue(&mut self, item: T) { + self.list.push_back(item); + } + + fn size(&self) -> usize { + self.list.len() + } +} + +fn main() { + let mut i32queue = Queue::new(); + + i32queue.enqueue(4); + i32queue.enqueue(5); + i32queue.enqueue(6); + + println!("{:?}", i32queue.dequeue().unwrap()); // 4 + println!("{:?}", i32queue.size()); // 2 + println!("{:?}", i32queue.front().unwrap()); // 5 +} diff --git a/contents/stacks_and_queues/code/rust/SConscript b/contents/stacks_and_queues/code/rust/SConscript new file mode 100644 index 000000000..0c3f15913 --- /dev/null +++ b/contents/stacks_and_queues/code/rust/SConscript @@ -0,0 +1,10 @@ +Import('*') +from pathlib import Path + +dirname = Path.cwd().parents[1].stem + +env.rustc(f'#/build/rust/stack', '#/contents/stacks_and_queues/code/rust/Stack.rs') +env.Clean('rust', f'#/build/rust/stack.pdb') + +env.rustc(f'#/build/rust/queue', '#/contents/stacks_and_queues/code/rust/Queue.rs') +env.Clean('rust', f'#/build/rust/queue.pdb') \ No newline at end of file diff --git a/contents/stacks_and_queues/code/rust/Stack.rs b/contents/stacks_and_queues/code/rust/Stack.rs new file mode 100644 index 000000000..13010c3f0 --- /dev/null +++ b/contents/stacks_and_queues/code/rust/Stack.rs @@ -0,0 +1,41 @@ +struct Stack { + list: Vec +} + +impl Stack { + fn new() -> Self { + Stack { + list: Vec::new(), + } + } + + // Note that this returns a reference to the value + // This is in contrast to pop which gives ownership of the value + fn top(&self) -> Option<&T> { + self.list.last() + } + + fn pop(&mut self) -> Option { + self.list.pop() + } + + fn push(&mut self, item: T) { + self.list.push(item); + } + + fn size(&self) -> usize { + self.list.len() + } +} + +fn main() { + let mut i32stack: Stack = Stack::new(); + + i32stack.push(4); + i32stack.push(5); + i32stack.push(6); + + println!("{:?}", i32stack.pop().unwrap()); // 6 + println!("{:?}", i32stack.size()); // 2 + println!("{:?}", i32stack.top().unwrap()); // 5 +} diff --git a/contents/stacks_and_queues/stacks_and_queues.md b/contents/stacks_and_queues/stacks_and_queues.md index 5d637deb3..a3c46b478 100644 --- a/contents/stacks_and_queues/stacks_and_queues.md +++ b/contents/stacks_and_queues/stacks_and_queues.md @@ -20,6 +20,8 @@ Here is a simple implementation of a stack: [import, lang:"typescript"](code/typescript/stack.ts) {% sample lang="java" %} [import, lang:"java"](code/java/Stack.java) +{% sample lang="rust" %} +[import, lang:"rust"](code/rust/Stack.rs) {% endmethod %} Here is a simple implementation of a queue: @@ -28,6 +30,8 @@ Here is a simple implementation of a queue: [import, lang:"typescript"](code/typescript/queue.ts) {% sample lang="java" %} [import, lang:"java" ](code/java/Queue.java) +{% sample lang="rust" %} +[import, lang:"rust" ](code/rust/Queue.rs) {% endmethod %}