File tree 1 file changed +17
-0
lines changed
1 file changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -181,6 +181,15 @@ pub fn quote(in_str: &str) -> Cow<str> {
181
181
}
182
182
}
183
183
184
+ /// Convenience function that consumes an iterable of words and turns it into a single string,
185
+ /// quoting words when necessary. Consecutive words will be separated by a single space.
186
+ pub fn join < ' a , I : IntoIterator < Item = & ' a str > > ( words : I ) -> String {
187
+ words. into_iter ( )
188
+ . map ( quote)
189
+ . collect :: < Vec < _ > > ( )
190
+ . join ( " " )
191
+ }
192
+
184
193
#[ cfg( test) ]
185
194
static SPLIT_TEST_ITEMS : & ' static [ ( & ' static str , Option < & ' static [ & ' static str ] > ) ] = & [
186
195
( "foo$baz" , Some ( & [ "foo$baz" ] ) ) ,
@@ -227,3 +236,11 @@ fn test_quote() {
227
236
assert_eq ! ( quote( "\" " ) , "\" \\ \" \" " ) ;
228
237
assert_eq ! ( quote( "" ) , "\" \" " ) ;
229
238
}
239
+
240
+ #[ test]
241
+ fn test_join ( ) {
242
+ assert_eq ! ( join( vec![ ] ) , "" ) ;
243
+ assert_eq ! ( join( vec![ "" ] ) , "\" \" " ) ;
244
+ assert_eq ! ( join( vec![ "a" , "b" ] ) , "a b" ) ;
245
+ assert_eq ! ( join( vec![ "foo bar" , "baz" ] ) , "\" foo bar\" baz" ) ;
246
+ }
You can’t perform that action at this time.
0 commit comments