When I use a static variable in generic functions, the entities of the variable in each instance of the generic function are all the same.
For instance, in this code
fn foo<T>() {
use std::sync::{Once, ONCE_INIT};
static INIT: Once = ONCE_INIT;
INIT.call_once(|| {
// run initialization here
println!("Called");
});
}
fn main() {
foo::<i64>();
foo::<i64>();
foo::<isize>();
}
println!
is called just once.
I had checked the assembly code using the Rust playground and saw that the INIT
variable is independent of which type T
actually is although foo<T>
themselves are instantiated with different name.
Is it possible for different instance of the generic function to have different static variables so that println!
is called twice in the above example?