相关疑难解决方法(0)

在Rust中打印用空格分隔的迭代器的惯用方式是什么?

我只想要一个空格String,这些空格与从中获取的参数变量分开,该变量std::env::args()是我一直使用以下fold函数创建的:

std::env::args()
    .fold("".to_string(), |accum, s| accum + &s + " ")
Run Code Online (Sandbox Code Playgroud)

然而,这在末端处产生了多余的空间,这是不需要的。我尝试使用该truncate函数,但未truncate返回a String,仅修改了现存的String,并且这将需要创建一个中间绑定,以便使用String的len()调用来定义被截断的长度String(这本身将需要我相信,由于Rust的当前词汇借用规则,中间绑定必不可少!)

rust

8
推荐指数
2
解决办法
3068
查看次数

为什么使用 join 时 Vec<&&str> 会出现“不满足的特征边界”?

我对此感到困惑:

use itertools::Itertools; // 0.10.0

fn main() {
    let combos = ["a", "b", "c"].iter().combinations(2).collect::<Vec<_>>();
    println!("{:#?}", combos[0].join(""));
}
Run Code Online (Sandbox Code Playgroud)
error[E0599]: the method `join` exists for struct `Vec<&&str>`, but its trait bounds were not satisfied
   --> src/main.rs:5:33
    |
5   |       println!("{:#?}", combos[0].join(""));
    |                                   ^^^^ method cannot be called on `Vec<&&str>` due to unsatisfied trait bounds
    |
    = note: the following trait bounds were not satisfied:
            `Vec<&&str>: Iterator`
            which is required by `Vec<&&str>: Itertools`
            `<[&&str] as Join<_>>::Output = _`
            `[&&str]: Iterator`
            which is required …
Run Code Online (Sandbox Code Playgroud)

rust

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

标签 统计

rust ×2