此代码
#![feature(thread_local)]
#[thread_local]
pub static FOO: [&str; 1] = [ "Hello" ];
fn change_foo(s: &'static str) {
FOO[0] = s;
}
fn main() {
println!("{}", FOO[0]);
change_foo("Test");
println!("{}", FOO[0]);
}
产生我期望在调试中的行为:
Hello
Test
但在发布时,它产生:
Hello
Hello
(在操场上测试,我现在手头没有Rust编译器。)
但是,在发布中,如果我在函数中打印值:
#![feature(thread_local)]
#[thread_local]
pub static FOO: [&str; 1] = [ "Hello" ];
fn change_foo(s: &'static str) {
print!("");
FOO[0] = s;
}
fn main() {
println!("{}", FOO[0]);
change_foo("Test");
println!("{}", FOO[0]);
}
输出正常。
为什么会这样?优化是否过于激进?