如何使用type&'static str创建100个不同的标签?

use*_*953 3 rust

我正在尝试使用RefCell和创建图形Rc.我想在一个带有字符串标签的循环中创建100个节点.这是图表表示:

struct Node {
    datum: &'static str,
    edges: Vec<Rc<RefCell<Node>>>,
}

impl Node {
    fn new(datum: &'static str) -> Rc<RefCell<Node>> {
        Rc::new(RefCell::new(Node {
            datum: datum,
            edges: Vec::new(),
        }))
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我写的用于创建节点的循环:

for i in 0..100 {
    let a = Node::new(concat!("A", i));
    let b = Node::new(concat!("A", i + 1));
    {
        let mut mut_root = a.borrow_mut();
        mut_root.edges.push(b.clone());
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

error: expected a literal
  --> src/main.rs:17:40
   |
17 |         let a = Node::new(concat!("A", i));
   |                                        ^

error: expected a literal
  --> src/main.rs:18:40
   |
18 |         let b = Node::new(concat!("A", i + 1));
   |                                        ^^^^^
Run Code Online (Sandbox Code Playgroud)

tre*_*tcl 7

你不能.

生命周期是描述性的,而不是规定性的:你不能创建一个字符串并说"这个数据有'static生命周期".Rust会让您编写生命周期注释,'static以便编译器可以检查您的工作.在运行时循环中构建字符串的情况下,您无法安全地调用它,'static因为它不是.(另见这个问题.)

但这并不意味着你没有任何选择.

完全避免生命

可能你正在寻找的只是为了避免Node使用生命周期参数进行注释,例如Node<'a>.在这种情况下,只需使用String:

struct Node {
    datum: String,
    edges: Vec<Rc<RefCell<Node>>>,
}
Run Code Online (Sandbox Code Playgroud)

这是惯用的,很可能是最好的选择.如果您确定String在创建它之后永远不需要方法,并且您不想存储其容量,则可以调用String::into_boxed_strBox<str>来代替它.这与存储方式相同&'static str,但是在删除str时将回收堆的存储Node.

你甚至可以使用Rc<str>,这也是一个存储,&'static str但克隆比一个更便宜Box.

伪造 'static

如果你真的想要,你可以&'static str通过制造Box<str>和泄漏来"伪造"a - 即丢弃它的知识,Box以便它的后备记忆永远不会被回收.自Rust 1.26起,就可以使用Box::leak; 我将创建一个帮助函数来创建标签:

fn label(i: u32) -> &'static str {
    let s = format!("A{}", i);
    Box::leak(s.into_boxed_str())
}
for i in 0..100 {
    let a = Node::new(label(i));
    let b = Node::new(label(i+1));
    // ...
}
Run Code Online (Sandbox Code Playgroud)

这给你带来的主要是&引用Copy.但是,Node是不是Copy无论如何,所以为什么不使用Box

在编译时或之前生成标签

如果你需要一个实际 的字符串'static,它必须在编译时存在.据我所知,macro_rules宏不够强大,但你可以随时生成代码.这是一种可能性:

// This part can be generated by a build script, or in your editor:
static LABELS: [&'static str; 100] = [
    "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10", "A11", "A12", "A13", "A14",
    "A15", "A16", "A17", "A18", "A19", "A20", "A21", "A22", "A23", "A24", "A25", "A26", "A27",
    "A28", "A29", "A30", "A31", "A32", "A33", "A34", "A35", "A36", "A37", "A38", "A39", "A40",
    "A41", "A42", "A43", "A44", "A45", "A46", "A47", "A48", "A49", "A50", "A51", "A52", "A53",
    "A54", "A55", "A56", "A57", "A58", "A59", "A60", "A61", "A62", "A63", "A64", "A65", "A66",
    "A67", "A68", "A69", "A70", "A71", "A72", "A73", "A74", "A75", "A76", "A77", "A78", "A79",
    "A80", "A81", "A82", "A83", "A84", "A85", "A86", "A87", "A88", "A89", "A90", "A91", "A92",
    "A93", "A94", "A95", "A96", "A97", "A98", "A99", "A100",
];
for i in 0..100 {
    let a = Node::new(LABELS[i]);
    // ...
}
Run Code Online (Sandbox Code Playgroud)