Skip to content

Instantly share code, notes, and snippets.

@gembin
Last active May 5, 2022 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gembin/7b9b16d19568aedfebf8af35d7238b15 to your computer and use it in GitHub Desktop.
Save gembin/7b9b16d19568aedfebf8af35d7238b15 to your computer and use it in GitHub Desktop.
Rust: code snippets

Convert a Vec to Vec<&str>

let v: Vec<&str> = v.iter().map(AsRef::as_ref).collect();

Convert a collection of characters into a String

  1. chars.iter().collect::()
// Vec<char> -> String
let chars = vec!['a', 'b', 'c'];
let string = chars.iter().collect::<String>();

// [char] -> String
let chars = ['a', 'b', 'c'];
let string = chars.iter().collect::<String>();
  
// &[char] -> String
let slice_chars = &chars[..];
let string = slice_chars.iter().collect::<String>();
  1. chars.concat()
//  Vec<char> -> String  
let chars = vec!["a", "b", "c"];
let string = chars.concat();
  1. strings.join(separator)
let names = ["firstName", "lastName"];
let joined = names.join(", ");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment