Closed kpark-hrp closed 1 year ago
It appears that replacing const
with static
fixes the issue.
static CLIENT_ID: Lazy<String> = Lazy::new(|| Alphanumeric.sample_string(&mut rand::thread_rng(), 16));
But, shouldn't const
still behave the same way where the closure in Lazy
is only evaluated once and shared?
It appears that this behavior is documented.
Note that the variable that holds Lazy is declared as static, not const. This is important: using const instead compiles, but works wrong.
But to me, this feels like an unintended behavior which should not compile
Yeah, it’s a known language level issue without good solutions, see https://github.com/rust-lang/rust/issues/40543
const
is not named right, that more like macro in C, basically it's copy the expression and put it where you use it. That just a leçon that rust developer learn at some point, nothing is perfect.
For unit testing, I have a const
String
that is randomly generated and shared.But it appears that
Lazy
is being evaluated multiple times and returns differentString
each call. I have a very basic equality check unit test to show this.