dsalgo/
strconv.rs

1//! module name is following Go's package.
2
3pub fn str_to_byte_vec(s: &str) -> Vec<u8> {
4    s.bytes().collect::<Vec<_>>()
5}
6
7pub fn str_to_char_vec(s: &str) -> Vec<char> {
8    s.chars().collect::<Vec<_>>()
9}
10
11pub fn char_slice_to_string(s: &[char]) -> String {
12    s.iter().collect()
13}
14
15pub fn chars_to_byte_vec<I: Iterator<Item = char>>(
16    chars: I,
17    offset: u8,
18) -> Vec<u8> {
19    chars.map(|c| c as u8 - offset).collect::<Vec<_>>()
20}
21
22#[cfg(test)]
23
24mod tests {
25
26    #[test]
27
28    fn test_str_to_byte_vec() {
29        use super::*;
30
31        let s = "abc";
32
33        assert_eq!(str_to_byte_vec(s), vec![b'a', b'b', b'c'],);
34    }
35
36    #[test]
37
38    fn test_str_to_char_vec() {
39        use super::*;
40
41        let s = "abc";
42
43        assert_eq!(str_to_char_vec(s), vec!['a', 'b', 'c']);
44    }
45
46    #[test]
47
48    fn test_char_slice_to_string() {
49        use super::*;
50
51        let s = vec!['a', 'b', 'c'];
52
53        assert_eq!(char_slice_to_string(&s), "abc");
54    }
55
56    #[test]
57
58    fn test_chars_to_byte_vec() {
59        use super::*;
60
61        let s = "abc";
62
63        assert_eq!(chars_to_byte_vec(s.chars(), b'a'), vec![0, 1, 2]);
64    }
65}