rust - What is the equivalent of the join operator over a vector of Strings?

ID : 20023

viewed : 59

Tags : stringruststring

Top 5 Answer for rust - What is the equivalent of the join operator over a vector of Strings?

vote vote

98

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); } 

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.

vote vote

82

As mentioned by Wilfred, SliceConcatExt::connect has been deprecated since version 1.3.0 in favour of SliceConcatExt::join:

let joined = string_list.join("-"); 
vote vote

71

There is a function from the itertools crate also called join which joins an iterator:

extern crate itertools; // 0.7.8  use itertools::free::join; use std::fmt;  pub struct MyScores {     scores: Vec<i16>, }  impl fmt::Display for MyScores {     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {         fmt.write_str("MyScores(")?;         fmt.write_str(&join(&self.scores[..], &","))?;         fmt.write_str(")")?;         Ok(())     } }  fn main() {     let my_scores = MyScores {         scores: vec![12, 23, 34, 45],     };     println!("{}", my_scores); // outputs MyScores(12,23,34,45) } 
vote vote

70

vote vote

50

providedIn tells Angular that the root injector is responsible for creating an instance of the your Service. Services that are provided this way are automatically made available to the entire application and don't need to be listed in any module.

Service classes can act as their own providers which is why defining them in the @Injectable decorator is all the registration you need.

Top 3 video Explaining rust - What is the equivalent of the join operator over a vector of Strings?

Related QUESTION?