- 主题:如何计数?
有两个文件,main.rs和lib.rs。
lib.rs
log_count(count:i64)
main.rs
在wifi 连接成功的时候,进行计数x=x+1,并且调用log_count(x)
现在有这么几个方案:
1. main自己完成计数,比如声明一个全局变量?但是这个变量必须是unsafe的对吧?虽然我这个main只在wifi里阿尼金额成功的时候修改x。这个能否声明在那个调用的地方?
2.lib.rs里面实现计数,函数变为log_count()。main.rs不进行计数。但是也仍然要unsafe?
--
FROM 72.199.121.*
wifi 连接成功时会执行哪里的代码?
【 在 bihai 的大作中提到: 】
: 有两个文件,main.rs和lib.rs。
: lib.rs
: log_count(count:i64)
: ...................
--
FROM 58.33.81.*
wifi连接是这样的,在main里面,有一个函数负责处理用户按钮,
button_handling...
ret = wifi_connect(ssid, pass)....
if ret.is_ok {
x=x+1
log_count(x)
}
【 在 RunningOn 的大作中提到: 】
: wifi 连接成功时会执行哪里的代码?
:
--
FROM 72.199.121.*
搞个结构体不就行了…
【 在 bihai 的大作中提到: 】
: 有两个文件,main.rs和lib.rs。
: lib.rs
: log_count(count:i64)
: ...................
--
FROM 61.148.244.*
那你这段代码就行了啊,有什么问题么?
【 在 bihai 的大作中提到: 】
: wifi连接是这样的,在main里面,有一个函数负责处理用户按钮,
: button_handling...
: ret = wifi_connect(ssid, pass)....
: ...................
--
FROM 58.33.81.*
如果想实现一个全局的多读多写的计数器,rust怎么搞呢?
【 在 st123123 的大作中提到: 】
: 搞个结构体不就行了…
--
FROM 221.222.20.*
跨线程?mpsc channel就行
【 在 littleSram 的大作中提到: 】
: 如果想实现一个全局的多读多写的计数器,rust怎么搞呢?
--
FROM 114.242.250.*
谢谢,不知道singleton 在rust里的最佳实践有没有?
【 在 st123123 的大作中提到: 】
: 跨线程?mpsc channel就行
--
FROM 221.222.20.*
具体什么场景用singleton?
【 在 littleSram 的大作中提到: 】
: 谢谢,不知道singleton 在rust里的最佳实践有没有?
--
FROM 114.254.9.*
What is the best way to create and use a struct with only one instantiation in the system? Yes, this is necessary, it is the OpenGL subsystem, and making multiple copies of this and passing it around everywhere would add confusion, rather than relieve it.
The singleton needs to be as efficient as possible. It doesn't seem possible to store an arbitrary object on the static area, as it contains a Vec with a destructor. The second option is to store an (unsafe) pointer on the static area, pointing to a heap allocated singleton. What is the most convenient and safest way to do this, while keeping syntax terse?
https://stackoverflow.com/questions/27791532/how-do-i-create-a-global-mutable-singleton
我想到的就是比如连接数据的client,比较重。
【 在 st123123 的大作中提到: 】
: 具体什么场景用singleton?
--
FROM 114.249.21.*