Generic Structs in Rust - Possible on the IC?

Hello Folks - I’ve got a quick question for the Rust-Mega-Brains out there… I’m still trying to get my head around rust.

Is it possible to have generics in structs and save these into memory inside a canister? For example

pub struct ExampleData<T,U> {
    pub data_one: Vec<T>,
    pub data_two: Vec<U>, 
}

thread_local! {
    static RUNTIME_STATE: RefCell<ExampleData<T,U>> = RefCell::default();  // wont work!! 
}

My understanding is that using a static variable for runtime state does not allow generic types as the size is not known at compile time.

Is there any way around this - or must all structs on the IC be defined?

Thanks!

Hi @NS01, I’m not a Rust-Mega-Brain but I think I can help with this one. Generics can be used to create generic functions and data structures, e.g. ExampleData in your case, but the type must known when you use those functions and data structures. The issue with your code is not that the size is not known at compile time but that T and U don’t exist. If you change them to types that exist and can be stored then your code will work*.

*assuming you implement Default for ExampleData because RefCell::default() requires the inner type to be Default of course.