Did I created a thousand instances of static objects in these case?

If I have a code

for i in 0..1000{
	static NAME: &'static str = "Steve";
}

Did I unnecessarily created multiple instances of NAME?

NAME could be inside a certain function like this.

for i in 0..1000{
	some_function()
}


fn some_function(){
	static NAME: &'static str = "Steve";
}	

where it make sense to declare the static str there, while the function is called multiple times.
Did I as well, created a multiple instances of NAME in this case?

No, statics are only created once per declaration at compile time. Where you put one pretty much only controls what else can access it, as it obeys the scoping rules.

1 Like