相关疑难解决方法(0)

为什么借临时是合法的?

来自C++,我很惊讶这段代码在Rust中是有效的:

let x = &mut String::new();
x.push_str("Hello!");
Run Code Online (Sandbox Code Playgroud)

在C++中,你不能取一个临时的地址,一个临时的不会比它出现的表达式更长.

临时居住在Rust多久了?既然x只是借用,谁是字符串的所有者?

temporary rust

13
推荐指数
3
解决办法
1254
查看次数

使用构建器模式时,"借来的值不够长"

我有以下代码:

pub struct Canvas<'a> {
    width: isize,
    height: isize,
    color: Color,
    surface: Surface,
    texture: Texture,
    renderer: &'a Renderer,
}

impl<'a> Canvas<'a> {
    pub fn new(width: isize, height: isize, renderer: &'a Renderer) -> Canvas<'a> {
        let color = Color::RGB(0, 30, 0);
        let mut surface = core::create_surface(width, height);
        let texture = Canvas::gen_texture(&mut surface, width, height, color, renderer);
        Canvas {
            width: width,
            height: height,
            color: color,
            surface: surface,
            texture: texture,
            renderer: renderer,
        }
    }

    pub fn color(&mut self, color: Color) -> &mut Canvas<'a> …
Run Code Online (Sandbox Code Playgroud)

builder lifetime rust borrow-checker

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

为什么编译器告诉我考虑使用`let`绑定"?

我的错误是什么以及如何解决?

fn get_m() -> Vec<i8> {
    vec![1, 2, 3]
}

fn main() {
    let mut vals = get_m().iter().peekable();
    println!("Saw a {:?}", vals.peek());
}
Run Code Online (Sandbox Code Playgroud)

(游乐场)

编译器的错误表明"考虑使用let绑定" - 但我已经:

error[E0597]: borrowed value does not live long enough
 --> src/main.rs:6:45
  |
6 |     let mut vals = get_m().iter().peekable();
  |                    -------                  ^ temporary value dropped here while still borrowed
  |                    |
  |                    temporary value created here
7 |     println!("Saw a {:?}", vals.peek());
8 | }
  | - temporary value needs …
Run Code Online (Sandbox Code Playgroud)

rust borrow-checker

4
推荐指数
2
解决办法
1235
查看次数

"借来的价值不够长久"似乎归咎于错误的事情

我正在计算一个单词出现在麦克白的次数:

use std::io::{BufRead, BufReader};
use std::fs::File;
use std::collections::HashMap;

fn main() {
    let f = File::open("macbeth.txt").unwrap();
    let reader = BufReader::new(f);
    let mut counts = HashMap::new();

    for l in reader.lines() {
        for w in l.unwrap().split_whitespace() {
            let count = counts.entry(w).or_insert(0);
            *count += 1;
        }
    }

    println!("{:?}", counts);
}
Run Code Online (Sandbox Code Playgroud)

生锈barfs,说:

error[E0597]: borrowed value does not live long enough
  --> src/main.rs:14:9
   |
11 |         for w in l.unwrap().split_whitespace() {
   |                  ---------- temporary value created here
...
14 |         }
   |         ^ temporary value dropped …
Run Code Online (Sandbox Code Playgroud)

rust

4
推荐指数
2
解决办法
839
查看次数

错误 [E0716]:借用时临时值下降(生锈)

tl;drerror[E0716]: temporary value dropped while borrowed是一个困难且常见的问题,有一致的解决方案吗?


我遇到了困难的 rustc 错误

error[E0716]: temporary value dropped while borrowed
...

creates a temporary which is freed while still in use
Run Code Online (Sandbox Code Playgroud)

搜索Stackoverflow,有很多关于这个rust error的问题error[E0716]也许 Rust 专家可以为这个常见的新手问题提供一个通用的解决方案,一个足够好的解决方案,它也可以回答链接的问题(见下文)。

示例代码

一个简洁的代码示例来演示该问题(Rust Playground):

type Vec1<'a> = Vec::<&'a String>;

fn fun1(s1: &String, v1: &mut Vec1) {
    v1.insert(0, &s1.clone());
}

fn main() {
    let mut vec1 = Vec::new();
    let str1 = String::new();
    fun1(&str1, &mut vec1);
}
Run Code Online (Sandbox Code Playgroud)

结果:

error[E0716]: temporary value dropped while borrowed
 --> …
Run Code Online (Sandbox Code Playgroud)

rust

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

标签 统计

rust ×5

borrow-checker ×2

builder ×1

lifetime ×1

temporary ×1