相关疑难解决方法(0)

为什么我可以返回对本地文字但不是变量的引用?

为什么这段代码会编译?

fn get_iter() -> impl Iterator<Item = i32> {
    [1, 2, 3].iter().map(|&i| i)
}

fn main() {
    let _it = get_iter();
}
Run Code Online (Sandbox Code Playgroud)

[1, 2, 3]是一个局部变量并iter()借用它.此代码不应编译,因为返回的值包含对局部变量的引用.

reference rust borrow-checker

18
推荐指数
1
解决办法
1094
查看次数

如何使用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
查看次数

如何创建具有特定生命周期的可变String?

我有一个阅读文件内容的功能.我需要通过引用从这个函数返回内容,我只是无法弄清楚如何String在函数内创建具有一定生命周期的mutable .

fn main() {
    let filename = String::new();
    let content: &String = read_file_content(&filename);
    println!("{:?}", content);
}

fn read_file_content<'a>(_filename: &'a String) -> &'a String {
    let mut content: &'a String = &String::new();

    //....read file content.....
    //File::open(filename).unwrap().read_to_string(&mut content).unwrap();

    return &content;
}
Run Code Online (Sandbox Code Playgroud)

输出:

error[E0597]: borrowed value does not live long enough
  --> src/main.rs:8:36
   |
8  |     let mut content: &'a String = &String::new();
   |                                    ^^^^^^^^^^^^^ does not live long enough
...
14 | }
   | - temporary value only lives …
Run Code Online (Sandbox Code Playgroud)

rust

2
推荐指数
1
解决办法
176
查看次数

在测试函数上声明生命周期

我正在进行一项无法编译的测试:

#[test]
fn node_cost_dequeue() {
    let mut queue: BinaryHeap<&NodeCost> = BinaryHeap::new();
    let cost_1: NodeCost = NodeCost::new(1, 50, 0);
    let cost_2: NodeCost = NodeCost::new(2, 30, 0);
    let cost_3: NodeCost = NodeCost::new(3, 80, 0);
    queue.push(&cost_1);
    queue.push(&cost_2);
    queue.push(&cost_3);
    assert_eq!(2, (*queue.pop().unwrap()).id);
}
Run Code Online (Sandbox Code Playgroud)

结果是 error: cost_1 does not live long enough

附加信息"借款人的借款价值下降".

所以我尝试添加显式的生命周期注释.

#[test]
fn node_cost_dequeue() {
    let mut queue: BinaryHeap<&'a NodeCost> = BinaryHeap::new();
    let cost_1: NodeCost<'a> = NodeCost::new(1, 50, 0);
    let cost_2: NodeCost<'a> = NodeCost::new(2, 30, 0);
    let cost_3: NodeCost<'a> = NodeCost::new(3, 80, 0); …
Run Code Online (Sandbox Code Playgroud)

lifetime rust

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

标签 统计

rust ×4

borrow-checker ×1

lifetime ×1

reference ×1