在 Rust 中打印由空格分隔的迭代器的惯用方法是什么? [英] What's an idiomatic way to print an iterator separated by spaces in Rust?

查看:43
本文介绍了在 Rust 中打印由空格分隔的迭代器的惯用方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想要从 std::env::args() 获得的参数变量的空格分隔 String,我一直使用 fold 功能如下:

std::env::args().fold("".to_string(), |accum, s| accum + &s + " ")

然而,这会在末尾产生一个多余的空间,这是不需要的.我尝试使用 truncate 函数,但 truncate 不返回 String,只是修改现有的 String,并且这还需要创建一个中间绑定,以便使用 Stringlen() 调用来定义截断的 String 多长时间> 应该是(我相信,由于 Rust 当前的词汇借用规则,它本身需要一个中间绑定!)

解决方案

在 Rust 中打印所有命令行参数的惯用方法是什么?

fn main() {让 mut args = std::env::args();如果让 Some(arg) = args.next() {打印!("{}", arg);对于 args 中的 args {打印!({}",arg);}}}

或者使用 Itertools 的 formatformat_with:

使用 itertools::Itertools;//0.8.0fn 主(){println!("{}", std::env::args().format(" "));}

<块引用>

我只想要一个空格分隔的 String

fn args() ->细绳 {让 mut 结果 = String::new();让 mut args = std::env::args();如果让 Some(arg) = args.next() {result.push_str(&arg);对于 args 中的 args {结果.push(' ');result.push_str(&arg);}}结果}fn 主(){println!("{}", args());}

fn args() ->细绳 {让 mut 结果 = std::env::args().fold(String::new(), |s, arg| s + &arg + " ");结果.pop();结果}fn 主(){println!("{}", args());}

如果您使用 Itertools,您可以使用上面的 format/format_with 示例和 format! 宏.

join 也很有用:

使用 itertools::Itertools;//0.8.0fn args() ->细绳 {std::env::args().join(" ")}fn 主(){println!("{}", args());}

在其他情况下,您可能希望使用 <代码>穿插:

使用 itertools::Itertools;//0.8.0fn args() ->细绳 {std::env::args().intersperse(" ".to_string()).collect()}fn 主(){println!("{}", args());}

请注意,这不如其他选择有效,因为每次迭代都会克隆 String.

I just want a space separated String of the argument variables obtained from std::env::args(), which I've been creating using the fold function like this:

std::env::args()
    .fold("".to_string(), |accum, s| accum + &s + " ")

However this creates an extra space at the end which is unwanted. I tried using the truncate function, but truncate doesn't return a String, just modifies the existing String, and also this would require the creation of an intermediate binding in order to use the String's len() call to define how long the truncated String should be (which itself would require an intermediate binding due to Rust's current lexical borrowing rules I believe!)

解决方案

What's an idiomatic way to print all command line arguments in Rust?

fn main() {
    let mut args = std::env::args();

    if let Some(arg) = args.next() {
        print!("{}", arg);

        for arg in args {
            print!(" {}", arg);
        }
    }
}

Or better with Itertools' format or format_with:

use itertools::Itertools; // 0.8.0

fn main() {
    println!("{}", std::env::args().format(" "));
}

I just want a space separated String

fn args() -> String {
    let mut result = String::new();
    let mut args = std::env::args();

    if let Some(arg) = args.next() {
        result.push_str(&arg);

        for arg in args {
            result.push(' ');
            result.push_str(&arg);
        }
    }

    result
}

fn main() {
    println!("{}", args());
}

Or

fn args() -> String {
    let mut result = std::env::args().fold(String::new(), |s, arg| s + &arg + " ");
    result.pop();
    result
}

fn main() {
    println!("{}", args());
}

If you use Itertools, you can use the format / format_with examples above with the format! macro.

join is also useful:

use itertools::Itertools; // 0.8.0

fn args() -> String {
    std::env::args().join(" ")
}

fn main() {
    println!("{}", args());
}

In other cases, you may want to use intersperse:

use itertools::Itertools; // 0.8.0

fn args() -> String {
    std::env::args().intersperse(" ".to_string()).collect()
}

fn main() {
    println!("{}", args());
}

Note this isn't as efficient as other choices as a String is cloned for each iteration.

这篇关于在 Rust 中打印由空格分隔的迭代器的惯用方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆