Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created July 22, 2019 06:09
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 rust-play/6ea16971a7335e551e65168652b4990d to your computer and use it in GitHub Desktop.
Save rust-play/6ea16971a7335e551e65168652b4990d to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#![allow(unused)]
fn main() {
use std::path::{Path, PathBuf};
let path = Path::new("/test/haha/foo.txt/");
assert_eq!(path.strip_prefix("/"), Ok(Path::new("test/haha/foo.txt")));
assert_eq!(path.strip_prefix("/test"), Ok(Path::new("haha/foo.txt")));
assert_eq!(path.strip_prefix("/test/"), Ok(Path::new("haha/foo.txt")));
assert_eq!(path.strip_prefix("/test/haha/foo.txt"), Ok(Path::new("")));
assert_eq!(path.strip_prefix("/test/haha/foo.txt/"), Ok(Path::new("")));
assert_eq!(path.strip_prefix("test").is_ok(), false);
assert_eq!(path.strip_prefix("/haha").is_ok(), false);
let prefix = PathBuf::from("/test/");
assert_eq!(path.strip_prefix(prefix), Ok(Path::new("haha/foo.txt")));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment