home ~ projects ~ socials

Join Items Split In An Iter In Rust

This is how I'm splitting a string and reassemblinig it with join.

fn main() {
  let text = "alfa|bravo|charlie".to_string();
  let rejoined = text.split("|").collect::<Vec<&str>>().join("-");
  dbg!(rejoined);
}
Output:
[_active_nvim_run:4:3] rejoined = "alfa-bravo-charlie"

Notes

  • I know you could do this with find and replace. The demo is not for this specific use case, but for me to figure out how to do the join in general.
  • In my actual use case, I'm doing this with lines split from a string.
-- end of line --