easier/strings.rs
1pub trait ToStringsExtension {
2 /// Converts a slice of string slices to a vector of strings.
3 /// e.g. instead of
4 /// ```rust
5 /// ["1".to_string(), "2".to_string(), "3".to_string()];
6 /// ```
7 /// you can do
8 /// ```rust
9 /// use easier::prelude::*;
10 /// ["1", "2", "3"].strings();
11 /// ```
12 fn strings(&self) -> Vec<String>;
13}
14
15impl ToStringsExtension for [&str] {
16 fn strings(&self) -> Vec<String> {
17 self.iter().map(|s| s.to_string()).collect()
18 }
19}
20
21#[cfg(test)]
22mod tests {
23 use crate::prelude::*;
24
25 #[test]
26 fn strings() {
27 let vec: Vec<String> = ["1", "2", "3"].strings();
28 assert_eq!(
29 vec,
30 vec!["1", "2", "3"]
31 .iter()
32 .map(|s| s.to_string())
33 .collect::<Vec<String>>()
34 );
35 }
36}