小编Tur*_*est的帖子

如何将String分配给可变静态变量?

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

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();
  }
}
Run Code Online (Sandbox Code Playgroud)

错误:

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
Run Code Online (Sandbox Code Playgroud)

string static rust

4
推荐指数
1
解决办法
1529
查看次数

标签 统计

rust ×1

static ×1

string ×1