【发布时间】:2020-06-15 14:16:04
【问题描述】:
我需要静态变量(或与模块/文件关联的任何变量)和一个静态数组来将它们保存在同一个模块中。它们没有必要指向相同的内存。静态变量需要一个循环来初始化。这在 Rust 中可行吗?
在代码中,如下所示。
use std::collections::HashSet;
pub struct A {
char_lens: HashSet<u8>,
}
impl A {
pub(crate) fn new(s: &'static str) -> A {
let mut char_lens: HashSet<u8> = HashSet::new();
for s in s.split_whitespace() {
char_lens.insert(s.len() as u8);
}
A { char_lens }
}
}
static VAR_A1: A = A::new("some string 1");
static VAR_A2: A = A::new("some string 2");
static A_ARRAY: [A; 2] = [VAR_A1, VAR_A2];
上面的代码失败了,因为静态变量不能使用循环来初始化自己:
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
--> src/lib.rs:17:20
|
17 | static VAR_A1: A = A::new("some string 1");
| ^^^^^^^^^^^^^^^^^^^^^^^
我尝试使用 lazy_static 板条箱:
use lazy_static::lazy_static; // 1.4.0
use std::collections::HashSet;
pub struct A {
char_lens: HashSet<u8>,
}
impl A {
pub(crate) fn new(s: &'static str) -> A {
let mut char_lens: HashSet<u8> = HashSet::new();
for s in s.split_whitespace() {
char_lens.insert(s.len() as u8);
}
A { char_lens }
}
}
lazy_static! {
static ref VAR_A1: A = A::new("some string 1");
static ref VAR_A2: A = A::new("some string 2");
static ref A_ARRAY: [A; 2] = [VAR_A1, VAR_A2];
}
这现在失败了,因为lazy_static 在后台为静态变量生成了一个唯一的结构。现在VAR_A1 和VAR_A2 有不同的类型,没有办法引用数组的类型。
error[E0308]: mismatched types
--> src/lib.rs:21:35
|
21 | static ref A_ARRAY: [A; 2] = [VAR_A1, VAR_A2];
| ^^^^^^ expected struct `A`, found struct `VAR_A1`
【问题讨论】:
-
您是否需要
A1和A2作为单独的变量? -
@Shepmaster 需要是什么意思?它们代表两个不同的东西。
-
我的意思是:仅包含值的数组是否可以接受,而不定义
A1/A2? -
@Shepmaster 我需要变量和包含变量的数组。它们没有必要指向同一个内存。
-
您当前的代码无法工作,因为
[VAR_A1, VAR_A2]会将VAR_A1的所有权从其变量转移到数组中,从而使变量处于未定义状态。跨度>
标签: rust