advance/global-variable #728
Replies: 24 comments 22 replies
-
lazy_static 就是语言设计的bug,居然是靠社区处理的。 |
Beta Was this translation helpful? Give feedback.
-
我倒是一直想问, 既然全局变量那么多问题,而且Rust也不推荐使用。 那是有什么方案可以做到变量既不用全局,也可以全局使用的。 比如说: fn main 里面创建,然后一层层的传递到其他函数去? 最后随着main函数的结束而结束? |
Beta Was this translation helpful? Give feedback.
-
我运行“标准库中的 OnceCell”代码失败了,提示:Compiling test_rust v0.1.0 (/myproject/test_rust) For more information about this error, try
|
Beta Was this translation helpful? Give feedback.
-
全局ID生成器 这个例子在atomic加载、累加的步骤 是不是用SeqCst更好? |
Beta Was this translation helpful? Give feedback.
-
1.63版新增关于全局静态变量的特性可以完全替代lazy_static了 |
Beta Was this translation helpful? Give feedback.
-
fn generate_id()->usize{ 这个代码有点问题,fetch_add 方法返回的是前值,current_val 和next_id的值是一样的。应该是在fetch_add之后再通过load方法获取值后返回: |
Beta Was this translation helpful? Give feedback.
-
lazy_static! 宏和Box::new 更推荐使用哪个呢?why? |
Beta Was this translation helpful? Give feedback.
-
太好了,本章节没有扩充前就读完了,我真是个大聪明。。😂 |
Beta Was this translation helpful? Give feedback.
-
使用标准库中的 std::sync::Once static INIT: Once = Once::new(); struct Logger; impl Logger { fn initialize_logger() { fn get_logger() -> &'static Logger { fn main() {
} |
Beta Was this translation helpful? Give feedback.
-
越来越觉得rust在国内用不起来了 |
Beta Was this translation helpful? Give feedback.
-
https://stackoverflow.com/questions/27791532/how-do-i-create-a-global-mutable-singleton |
Beta Was this translation helpful? Give feedback.
-
现在 |
Beta Was this translation helpful? Give feedback.
-
std once cell changed: 它被移到了std::cell(现在名为LazyCell)。 https://doc.rust-lang.org/core/cell/struct.LazyCell.html
|
Beta Was this translation helpful? Give feedback.
-
最后这个例子中,Logger::log()方法,不是线程安全的吧?应该会出现一条log msg被中断的可能性。 |
Beta Was this translation helpful? Give feedback.
-
请问“编译器会尽可能将其内联到代码中”应该如何理解? |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
这鬼东西设计的这么复杂 |
Beta Was this translation helpful? Give feedback.
-
需要
…------------------ 原始邮件 ------------------
发件人: ***@***.***>;
发送时间: 2023年11月2日(星期四) 上午10:40
收件人: ***@***.***>;
抄送: ***@***.***>; ***@***.***>;
主题: Re: [sunface/rust-course] advance/global-variable (Discussion #728)
多线程共享的要用 Arc 包裹吗
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
需要
…------------------ 原始邮件 ------------------
发件人: ***@***.***>;
发送时间: 2023年11月2日(星期四) 上午10:40
收件人: ***@***.***>;
抄送: ***@***.***>; ***@***.***>;
主题: Re: [sunface/rust-course] advance/global-variable (Discussion #728)
多线程共享的要用 Arc 包裹吗
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Axum中,数据库(连接池)实例,用 State 共享,还是 OnceCell ? |
Beta Was this translation helpful? Give feedback.
-
官方教程的Object-Oriented Programming Features of Rust这章没有吗? |
Beta Was this translation helpful? Give feedback.
-
版本1.80+标准库有解决方案了 use std::sync::LazyLock;
// n.b. static items do not call [`Drop`] on program termination, so this won't be deallocated.
// this is fine, as the OS can deallocate the terminated program faster than we can free memory
// but tools like valgrind might report "memory leaks" as it isn't obvious this is intentional.
static DEEP_THOUGHT: LazyLock<String> = LazyLock::new(|| {
// M3 Ultra takes about 16 million years in --release config
another_crate::great_question()
});
// The `String` is built, stored in the `LazyLock`, and returned as `&String`.
let _ = &*DEEP_THOUGHT;
// The `String` is retrieved from the `LazyLock` and returned as `&String`.
let _ = &*DEEP_THOUGHT; |
Beta Was this translation helpful? Give feedback.
-
我现在的版本是rustc 1.80.0,现在已经可以通过LazyLock和OnceLock设置静态变量了 |
Beta Was this translation helpful? Give feedback.
-
以及下面的示例是不是矛盾了,这个 static REQUEST_RECV: AtomicUsize = AtomicUsize::new(0); |
Beta Was this translation helpful? Give feedback.
-
advance/global-variable
https://course.rs/advance/global-variable.html
Beta Was this translation helpful? Give feedback.
All reactions