0

In the following code, I want automatically initialize my array:

struct ColorQueue {
    queue: String,
    color: String,
}

impl ColorQueue {
    pub fn new(queue: &str) -> Self {
        Self {
            queue: String::from(queue),
            color: "".to_string(),
        }
    }

    fn name_colors(&self) {
        let colorqueue = [
            ColorQueue {
                queue: "amig".to_string(),
                color: "HRED".to_string(),
            },
            ColorQueue {
                queue: "micmac".to_string(),
                color: "GRN".to_string(),
            },
            ColorQueue {
                queue: "ssfa".to_string(),
                color: "YEL".to_string(),
            },
            ColorQueue {
                queue: "chrody".to_string(),
                color: "BLU".to_string(),
            },
            ColorQueue {
                queue: "ngs".to_string(),
                color: "MAG".to_string(),
            },
            ColorQueue {
                queue: "emc2".to_string(),
                color: "CYN".to_string(),
            },
            ColorQueue {
                queue: "cryoem".to_string(),
                color: "WHT".to_string(),
            },
            ColorQueue {
                queue: "common".to_string(),
                color: "BWHT".to_string(),
            },
            ColorQueue {
                queue: "lowprio".to_string(),
                color: "RED".to_string(),
            },
            ColorQueue {
                queue: "bim".to_string(),
                color: "BCYN".to_string(),
            },
            ColorQueue {
                queue: "runxx".to_string(),
                color: "BBLU".to_string(),
            },
        ];
    }
    pub fn set_queue_name(&mut self, queue: String) {
        self.queue = queue;
    }
    pub fn get_color(&self) -> &String {
        for item in 1..self.colorqueue.len() {
            if self.queue == self.colorqueue[item].queue {
                return &self.colorqueue[item].color;
            }
        }
    }
    pub fn get_queue_name(&self) -> &String {
        return &self.queue;
    }
}

fn main() {
    let mut cqueue = ColorQueue::new(&"amig");
    ColorQueue::set_queue_name("amig".to_string());
    println!("{}", cqueue.get_queue_name());
    cqueue.set_queue_name("ngs".to_string());
    println!("{}", cqueue.get_queue_name())
}

For now I can assign a queue. What I want is a function get_color(queue) that returns the color field, depending on queue name.

This should return "MAG":

cqueue.set_queue_name("ngs".to_string());
cqueue.get_color();

But I must initialize my array in the object.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • As it is now, `colorqueue` gets deallocated after name_colors and isn't able to be used, so I presume this is the problem you're trying to solve. You say "But I must initialize my array in the object". Why is that? What are you trying to accomplish by not, for instance, making `colorqueue` a top-level `const`? – Connor Mooneyhan Feb 01 '22 at 14:28
  • Hi Thank you for your comment. sure I can do top-level const, and I will implement it. It's for my own knowledge. And Why ? The aim it hid e all this part inside object because I don't need to know about my array in main() , I only need 'queue name' , and a result at the end. And finally build an independant module. Best Regards. – Lezar Kein Feb 01 '22 at 14:58
  • I appreciate the urge to save memory, but if you want this to be globally accessible, it sounds like it's not too unreasonable to declare `colorqueue` globally. Perhaps, if you envision this program growing and you only need `colorqueue` for part of it, you could declare it within the scope in which you'll use it so it will get dropped when it's no longer needed. – Connor Mooneyhan Feb 01 '22 at 15:18

1 Answers1

0

In essence you want to get a predefined color for a given queue ID. First, let's make a place for the default colors. I'd make a module for that:

pub mod queue_default_colors { ... }

and in there have a private static variable:

static default_colors: &[(&'static str, &'static str)] = &[
    ("amig", "HRED"),
    ("micmac", "GRN"),
    ...
];

And based on that implement your function:

pub fn get(queue_id: &str) -> Option<&'static str> {
    default_colors.iter()
        .find(|pair| pair.0 == queue_id)
        .map(|pair| pair.1)
}

Note that it returns Option, because queue_id can be custom (unmapped).

This can be used like so:

let color = queue_default_colors::get("amig"); // Some("HRED")

It is possible to optimize this if you change default_colors to a HashMap, but then you need to initialize it. See here for the options - https://stackoverflow.com/a/32956193/1009546

battlmonstr
  • 5,841
  • 1
  • 23
  • 33