简体   繁体   English

如何在 static mut array/vec 中设置字符串值?

[英]How can I set string values in a static mut array/vec?

I have a symbol table that I want to be static because it is accessed in a lot of circumstances where I don't have a good way to pass the value in. It is a struct and a table that looks like:我有一个我想成为 static 的符号表,因为它在很多情况下都可以访问,而我没有很好的方法来传递值。它是一个结构和一个看起来像的表:

struct Symbol {
   string: &'static str,
   other_values: ...,
}
const NO_SYMBOL = Symbol{ string: "", other...};
static mut symbol_table : [Symbol;100] = [NO_SYMBOL;100];

How do I update the string field?如何更新字符串字段? It has to be static, because it's in a static array, but I want to generate values (among other things, by reading from a file), so how can I make a String static so I can store it in an element of the array?它必须是 static,因为它在 static 数组中,但我想生成值(除其他外,通过从文件中读取),所以我怎样才能在数组中创建字符串 ZA81259CEF8E959C624ZD1D45E ?

There are a couple of ways to do this, depending on what you think is best.有几种方法可以做到这一点,具体取决于您认为最好的方法。 Use a Vec :使用Vec

static mut symbol_table_1:Vec<Symbol> = Vec:new(); //doesn't actually allocate so is a const fn

or use an option and replace the none values:或使用选项并replace none 值:

static mut symbol_table_2:[Option<Symbol>;100] = [None;100];

unsafe{
    symbol_table_2[0].replace(Some(value));
}

A &'static str can be obtained by using String::into_boxed_str and Box::leak . &'static str可以通过使用String::into_boxed_strBox::leak获得。 However, I would highly encourage you to use lazy_static and an RwLock , which enables many readers or one writer to maintain a lock on the static and prevent data races.但是,我强烈建议您使用lazy_staticRwLock ,这使得许多读取器或一个写入器能够保持对static的锁定并防止数据竞争。

lazy_static!{
    static ref symbol_table_3:Rwlock<Vec<Symbol>> = RwLock::new(Vec::new());
}

Full playground link 完整的游乐场链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能转换Vec <T> 进入C友好* mut T? - How can I convert a Vec<T> into a C-friendly *mut T? 如何从 &amp;mut Nested 创建 &amp;mut MyEnum? - How can I create a &mut MyEnum from &mut Nested? 如何在不获取 E0507 的情况下将包含 Vec 的结构移入/移出 static 数组? - How can I move structs containing Vec to/from a static array without getting E0507? 我怎样才能转换 Vec<string> 到 &[&str]?</string> - How can I convert Vec<String> to &[&str]? 如何从 wasm (rust) 到 js 获取 static mut 的地址? - How can I get the adress of a static mut from wasm (rust) to js? 为什么在索引到不可变的 Vec 后不能调用borrow_mut()<RefCell> ? - Why can I not call borrow_mut() after indexing into an immutable Vec<RefCell>? 你如何将 Vec&lt;&amp;mut T&gt; 转换为 Vec&lt;&amp;T&gt;? - How do you convert Vec<&mut T> to Vec<&T>? 如何转换 Clamped <vec<u8> &gt; 用 Rust 和 web-sys 夹紧&lt;&amp;mut [u8]&gt;? </vec<u8> - How do I convert Clamped<Vec<u8>> to Clamped<&mut [u8]> with Rust and web-sys? HashMap 中的变异向量元素<String, Vec<&mut String> &gt; - Mutating vector elements in HashMap<String, Vec<&mut String>> 如何将 &amp;mut ndarray::Array 传递给函数并使用它执行逐元素算术? - How can I pass an &mut ndarray::Array to a function and perform element-wise arithmetic with it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM