Is it possible to have a mutable non-static global variable in Rust?
Say I wanted this:
pub let foo: u64 = 47;
This will not work, as rust does not allow the let
keyword outside of a function.
Is it possible to have a mutable non-static global variable in Rust?
Say I wanted this:
pub let foo: u64 = 47;
This will not work, as rust does not allow the let
keyword outside of a function.
Create a container structure:
pub struct FooContainer {
pub foo: u64,
}
pub const foo: FooContainer = FooContainer { foo: 47 };
You can now access it via
mod foovar;
pub main() {
println!("{}", foovar::foo.foo)
}
Your instantiation of the struct can remain const
since it's field foo
is still mut
.