如何在 Rust 中为返回的元组设置静态生命周期?
How do I set a static lifetime to a returned tuple in Rust?
假设我有以下构造函数 returns 一个元组:
pub struct WebCam {
id: u8
}
impl WebCam {
fn new() -> (Self, bool) {
let w = WebCam {id: 1 as u8};
return (w, false);
}
}
pub fn main() {
static (cam, isRunning): (WebCam, bool) = WebCam::new();
}
以上代码无法编译。但是,如果我将 static
更改为 let
,它就可以正常编译。我不确定如何为返回的元组的各个值设置生命周期(在一行中)?
https://doc.rust-lang.org/reference/items/static-items.html
您可能会混淆是应该使用常量项还是静态项。一般来说,常量应该优于静态,除非满足以下条件之一:
正在存储大量数据
需要静态单地址属性
需要内部可变性。
A static item is similar to a constant, except that it represents a precise memory location in the program. All references to the static refer to the same memory location. Static items have the static lifetime, which outlives all other lifetimes in a Rust program. Non-mut static items that contain a type that is not interior mutable may be placed in read-only memory. Static items do not call drop at the end of the program.
All access to a static is safe, but there are a number of restrictions on statics:
The type must have the Sync trait bound to allow thread-safe access.
Statics allow using paths to statics in the constant expression used to initialize them, but statics may not refer to other statics by value, only through a reference.
Constants cannot refer to statics.
我会像这样重写你的代码:
pub struct WebCam {
id: u8,
}
impl WebCam {
fn new() -> (Self, bool) {
(WebCam { id: 1u8 }, false)
}
}
pub fn main() {
let (cam, is_running) = WebCam::new();
println!("{} {}", cam.id, is_running);
}
这也有效:
pub struct WebCam {
id: u8,
}
impl WebCam {
fn new() -> (Self, bool) {
(WebCam { id: 1u8 }, false)
}
}
static mut IS_RUNNING: bool = false;
static mut WEB_CAM: WebCam = WebCam { id: 0u8 };
pub fn main() {
let (cam, is_running) = WebCam::new();
unsafe {
IS_RUNNING = is_running;
WEB_CAM.id = cam.id;
}
println!("{} {}", cam.id, is_running);
}
假设我有以下构造函数 returns 一个元组:
pub struct WebCam {
id: u8
}
impl WebCam {
fn new() -> (Self, bool) {
let w = WebCam {id: 1 as u8};
return (w, false);
}
}
pub fn main() {
static (cam, isRunning): (WebCam, bool) = WebCam::new();
}
以上代码无法编译。但是,如果我将 static
更改为 let
,它就可以正常编译。我不确定如何为返回的元组的各个值设置生命周期(在一行中)?
https://doc.rust-lang.org/reference/items/static-items.html
您可能会混淆是应该使用常量项还是静态项。一般来说,常量应该优于静态,除非满足以下条件之一:
正在存储大量数据
需要静态单地址属性
需要内部可变性。
A static item is similar to a constant, except that it represents a precise memory location in the program. All references to the static refer to the same memory location. Static items have the static lifetime, which outlives all other lifetimes in a Rust program. Non-mut static items that contain a type that is not interior mutable may be placed in read-only memory. Static items do not call drop at the end of the program.
All access to a static is safe, but there are a number of restrictions on statics:
The type must have the Sync trait bound to allow thread-safe access. Statics allow using paths to statics in the constant expression used to initialize them, but statics may not refer to other statics by value, only through a reference. Constants cannot refer to statics.
我会像这样重写你的代码:
pub struct WebCam {
id: u8,
}
impl WebCam {
fn new() -> (Self, bool) {
(WebCam { id: 1u8 }, false)
}
}
pub fn main() {
let (cam, is_running) = WebCam::new();
println!("{} {}", cam.id, is_running);
}
这也有效:
pub struct WebCam {
id: u8,
}
impl WebCam {
fn new() -> (Self, bool) {
(WebCam { id: 1u8 }, false)
}
}
static mut IS_RUNNING: bool = false;
static mut WEB_CAM: WebCam = WebCam { id: 0u8 };
pub fn main() {
let (cam, is_running) = WebCam::new();
unsafe {
IS_RUNNING = is_running;
WEB_CAM.id = cam.id;
}
println!("{} {}", cam.id, is_running);
}