Skip to content

Instantly share code, notes, and snippets.

@aalvarado aalvarado/
Created Feb 23, 2019

What would you like to do?
Naive circular queue in Rust
use std::fmt;
struct CircularQueue<T> {
max: usize,
list: Vec<T>,
impl <T> CircularQueue<T> where T: std::string::ToString {
pub fn new(max: usize) -> CircularQueue<T> {
CircularQueue {
max: max,
list: Vec::new(),
pub fn enq(&mut self, number: T) {
// Not really a `shift` here. The way remove works, is
// by removing the element and then shifting everything left.
pub fn deq(&mut self) -> T {
impl <T> fmt::Display for CircularQueue<T> where T: std::string::ToString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{}]",
self.list.iter().fold(String::new(), |acc, num| acc + &num.to_string() + ", ")
fn main() {
let mut cq = CircularQueue::new(7);
println!("{}", cq);
println!("{}", cq);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.