字符串向量上的连接运算符的等价物是什么? [英] What is the equivalent of the join operator over a vector of Strings?
本文介绍了字符串向量上的连接运算符的等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法在 String
向量上找到join"运算符的 Rust 等效项.我有一个 Vec
,我想将它们作为一个 String
加入:
I wasn't able to find the Rust equivalent for the "join" operator over a vector of String
s. I have a Vec<String>
and I'd like to join them as a single String
:
let string_list = vec!["Foo".to_string(),"Bar".to_string()];
let joined = something::join(string_list,"-");
assert_eq!("Foo-Bar", joined);
相关:
推荐答案
在 Rust 1.3.0 及更高版本中,join
可用:
In Rust 1.3.0 and later, join
is available:
fn main() {
let string_list = vec!["Foo".to_string(),"Bar".to_string()];
let joined = string_list.join("-");
assert_eq!("Foo-Bar", joined);
}
在 1.3.0 之前这个方法被称为 connect
:
Before 1.3.0 this method was called connect
:
let joined = string_list.connect("-");
请注意,您不需要导入任何内容,因为这些方法是由 标准库前奏.
Note that you do not need to import anything since the methods are automatically imported by the standard library prelude.
这篇关于字符串向量上的连接运算符的等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文