小编Leh*_*enn的帖子

在总结命令行参数时,"无法摆脱借来的内容"

这是我的第一个Rust程序,似乎我已经遇到了可怕的借用检查程序.:)

程序应该读取命令行中传递的参数,对它们求和并返回结果.我很难将参数解析为整数.

use std::env;

fn main() {
    let args: Vec<String> = env::args().collect();
    let sum_args: i32 =
        args
        .iter()
        .skip(1)
        .fold(0, |a, &b| a + b.parse::<i32>().ok().expect("Not an i32!"));
    println!("{:?}", sum_args.to_string());
}
Run Code Online (Sandbox Code Playgroud)

哪个失败了:

error[E0507]: cannot move out of borrowed content
 --> src/main.rs:9:22
  |
9 |         .fold(0, |a, &b| a + b.parse::<i32>().ok().expect("Not an i32!"));
  |                      ^-
  |                      ||
  |                      |hint: to prevent move, use `ref b` or `ref mut b`
  |                      cannot move out of borrowed content
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

rust borrow-checker

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

标签 统计

borrow-checker ×1

rust ×1