小编use*_*953的帖子

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

我正在尝试使用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 …
Run Code Online (Sandbox Code Playgroud)

rust

3
推荐指数
1
解决办法
97
查看次数

标签 统计

rust ×1