In Rust, you can't use enum variants directly as field names in a struct. However, you can create a struct that holds a map or multiple fields, each corresponding to a variant of the enum. Here's an example of how you might represent a Bucket that contains vectors corresponding to some variants of the Pocket enum:
#[derive(Debug, Eq, Hash, PartialEq)]
pub enum Pocket {
Pass,
InRet,
Partial,
Unmapped,
}
#[derive(Debug, Clone)]
pub struct Bucket {
pass: Vec<String>,
in_ret: Vec<String>,
// You can also include other fields for the other variants if needed
// partial: Vec<String>,
// unmapped: Vec<String>,
}
impl Bucket {
pub fn new() -> Self {
Bucket {
pass: Vec::new(),
in_ret: Vec::new(),
// Initialize other fields if added
// partial: Vec::new(),
// unmapped: Vec::new(),
}
}
pub fn insert(&mut self, pocket: Pocket, value: String) {
match pocket {
Pocket::Pass => self.pass.push(value),
Pocket::InRet => self.in_ret.push(value),
// Handle other variants if needed
// Pocket::Partial => self.partial.push(value),
// Pocket::Unmapped => self.unmapped.push(value),
}
}
// You can add getter methods to access the vectors if needed
pub fn pass(&self) -> &Vec<String> {
&self.pass
}
pub fn in_ret(&self) -> &Vec<String> {
&self.in_ret
}
// And so forth for other fields
}
In this example, Bucket has a field for each enum variant you want to store (Pass and InRet). I've also included comments on how you might handle the other variants if necessary.
I've deliberately omitted fields for Partial and Unmapped, based on the struct you've shown in the question, but you can easily add them if required.
The insert method takes a Pocket variant and a String, adding the string to the corresponding vector based on which pocket it is. Accessor methods for pass and in_ret are provided to retrieve references to the stored vectors. If you add more fields, you can also provide corresponding accessor methods.
Remember that struct field names in Rust must adhere to the variable naming conventions, which means they can't contain symbols like :: and need to be valid identifiers. That's why pass and in_ret are used as the field names, rather than Pocket::Pass and Pocket::InRet.
If you need to store all variants of the enum, with the ability to dynamically determine which variant a particular piece of data corresponds to, you might consider using a HashMap<Pocket, Vec<String>> instead.