如何将字符串分配给可变静态变量? [英] How do I assign a String to a mutable static variable?

查看:33
本文介绍了如何将字符串分配给可变静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想给一个全局变量赋值,但它一直有编译器错误:

I want to assign a value to a global variable, but it keeps having a compiler error:

static mut NameArr: [&'static str; 20] = ["\0"; 20];

fn main() {

  unsafe {
    static mut S1 :String = "".to_string();

    S1.push('\0');

    NameArr[0] = S1.as_slice();
  }
}

错误:

a.rs:7:29: 7:43 error: mutable statics are not allowed to have destructors
a.rs:7     static mut S1 :String = "".to_string();
                                   ^~~~~~~~~~~~~~
a.rs:7:29: 7:43 error: static contains unimplemented expression type [E0019]
a.rs:7     static mut S1 :String = "".to_string();
                                   ^~~~~~~~~~~~~~
error: aborting due to 2 previous errors

推荐答案

不知道你想做什么,所以我不能告诉你怎么做.

I have no idea what you are trying to do, so I can't tell you how to do it.

也许您想要如何创建全局可变单例?

话虽如此,我可以帮助解释您的错误:

That being said, I can help explain your errors:

static mut NameArr: [&'static str; 20] = ["\0"; 20];

这声明了一个可变的全局变量.该变量是一个固定长度的 20 个项目的数组.每个项目都是一个 &'static str,一个字符串文字,必须保证在整个程序的生命周期中都存在.这就是 'static 的意思.

This declares a mutable global variable. The variable is an array of a fixed-length of 20 items. Each item is a &'static str, a string literal that must be guaranteed to live for the life of the entire program. That's what 'static means.

static mut S1: String = "".to_string();

这试图创建另一个可变全局变量,这次它是一个String,一个在堆中分配并拥有一块内存的对象.当 String 超出范围时,内存会通过 析构函数 释放.这是您的第一个错误的根源 - 不允许全局变量具有析构函数.此外,当前不允许您在定义全局值时调用方法 - 这些方法现在没有运行的上下文!

This attempts to create another mutable global variable, this time it is a String, an object that allocates and owns a piece of memory in the heap. When a String goes out of scope, the memory is freed via a destructor. This is the source of your first error - global variables aren't allowed to have destructors. Additionally, you aren't currently allowed to call methods when defining a global value - there's no context for these methods to run right now!

拥有全局可变变量对于 Rust 或任何程序来说真的不是一个好主意,真的.有技术原因(例如数据竞争)和程序员原因(对这种代码进行合理化是困难).你可以查看如何创建一个全局的、可变的单例?,如果你真的需要它,你可以如何做的说明.

Having global mutable variables is really not a good idea with Rust, or any program, really. There's technical reasons (such as data races) and programmer reasons (rationalizing about this kind of code is hard). You can check out How do I create a global, mutable singleton? for instructions on how to do it if you really need it.

Rust 强制您使用 unsafe 块来处理可变的全局变量.编译器无法再保证程序的安全性.例如,如果您要调用 S1.clear,您存储在数组中的值会发生什么变化?

Rust forces you to use an unsafe block to deal with mutable global variables for these reasons. The compiler can no longer guarantee the safety of your program. For example, what happens to the value you've stored in the array if you were to call S1.clear?

还有文体问题:

  1. Rust 使用 snake_case 作为变量
  2. SCREAMING_SNAKE_CASE 用于常量/静态
  3. CamelCase 用于结构/枚举/特征
  4. 4 个空格缩进
  5. 变量定义应该是name: type:
  6. 后面有空格
  1. Rust uses snake_case for variables
  2. SCREAMING_SNAKE_CASE for constants / statics
  3. CamelCase for structs/enums/traits
  4. 4-space indention
  5. Variable definitions should be name: type, with the space after the :

这篇关于如何将字符串分配给可变静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆