I know this from RFC 246:
- constants declare constant values. These represent a value, not a memory address. This is
The main purpose of static is to allow functions to control an internal value that is remembered across calls, but not be accessible by the main application code. It is similar to Class variables as opposed to Instance variables in other languages. Also C and PHP and many other languages have this concept.
Example: you want to track how many times a function is called and have a way of resetting the internal counter:
fn counter(reset: bool) -> i32 {
static mut Count: i32 = 0;
unsafe {
if reset {
Count = 0;
}
Count += 1;
return Count;
}
}
println!("{}",counter(true));
println!("{}",counter(false));
println!("{}",counter(false));
//println!("{}", Count); // Illegal