-
I have a struct like: struct Date<'a, T: Display> {
format: String,
formatter: Vec<FormatItem<'a>>,
earliest: Option<T>,
latest: Option<T>,
} Given this struct, I want to store the result of fn new(method: AggregationMethod) -> Self {
if let AggregationMethod::Date(format_string) = method {
match parse(&format_string) {
Ok(formatter) => {
let parser: Date<'a, T> = Date {
format: format_string.to_owned(),
formatter,
earliest: None,
latest: None,
};
return parser;
}
Err(why) => panic!(why),
}
} else {
todo!();
};
} For context, The problem with my code above is that I thought that I might be able to construct the fn new(method: AggregationMethod) -> Self {
if let AggregationMethod::Date(format_string) = method {
let mut parser: Date<'a, T> = Date {
format: format_string,
formatter: None,
earliest: None,
latest: None,
};
match parse(&parser.format) {
Ok(formatter) => {
parser.formatter = Some(formatter);
return parser;
}
Err(why) => panic!(why),
}
} else {
panic!("Date aggregator constructed with non-date AggregationMethod!")
};
} If it is useful, here is the compiler output of this second block of code:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Given that you mentioned you want the struct to own everything, this is not possible. Self-referential structs don't exist in Rust, and that's what would be required — the I don't think this is the case given what you've stated, but if the format description is statically-known, you can use the |
Beta Was this translation helpful? Give feedback.
Given that you mentioned you want the struct to own everything, this is not possible. Self-referential structs don't exist in Rust, and that's what would be required — the
FormatItem::Literal
variant would point to the contents of theformat
field.I don't think this is the case given what you've stated, but if the format description is statically-known, you can use the
time::macros::format_description
macro.