1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// @Title: 替换空格 (替换空格 LCOF)
// @Author: 15816537946@163.com
// @Date: 2022-02-02 21:19:37
// @Runtime: 0 ms
// @Memory: 2.1 MB

impl Solution {
    pub fn replace_space(s: String) -> String {
        let mut ans = vec![];
        s.chars().for_each(|x| {
            match x {
                ' ' => ans.append(&mut vec!['%', '2', '0']),
                _ => ans.push(x),
            };
        });
        ans.iter().collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_case1() {
        let s = String::from("We are happy.");
        assert_eq!(Solution::replace_space(s), "We%20are%20happy.");
    }
}