How to cache Option Some value and is there some Reset or Clear trait?

⚓ Rust    📅 2026-03-06    👤 surdeus    👁️ 3      

surdeus

I have the following struct:

#[derive(Debug, Clone, Default)]
pub struct Scope {
    pub name: String,
    pub name_for: Option<String>,
    pub type_of_scope: ScopeType,
}

It holds the current iteration scope value. I want to reuse it on the next iteration. Currently, I just recreate it using scope = Default::default();, however, considering that my loop has several millions iterations, it would me more efficient to use something like: scope.clear();. However, I didn't find any standard Clear or Reset traits. Not a big deal, tho, because I can create my own Clear trait, but one item remains unsolved. name_for may contain Some(String) and I do not want to destroy it even if I assigned None to it. I simply want to reuse the last cleared Some value when I push new chars in it . How can I do that?

5 posts - 4 participants

Read full topic

🏷️ Rust_feed