LeetCode/rs/simplify-path.rs
Matej Focko 2351dfd0ee
chore: unwrap one layer
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-12-12 14:36:00 +01:00

49 lines
1,009 B
Rust

struct Solution {}
impl Solution {
pub fn simplify_path(path: String) -> String {
let mut simplified_path = Vec::<&str>::new();
path.split('/').for_each(|seg| {
if seg.is_empty() || seg == "." {
return;
}
if seg == ".." {
simplified_path.pop();
return;
}
simplified_path.push(seg);
});
"/".to_owned() + &simplified_path.join("/")
}
}
fn main() {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one_directory() {
assert_eq!(
Solution::simplify_path("/home/".to_owned()),
"/home".to_owned()
);
}
#[test]
fn just_root() {
assert_eq!(Solution::simplify_path("/../".to_owned()), "/".to_owned());
}
#[test]
fn consecutive_slashes() {
assert_eq!(
Solution::simplify_path("/home//foo/".to_owned()),
"/home/foo".to_owned()
);
}
}