我正在尝试从Windows路径中删除前缀。我尝试使用strip_prefix
方法执行此操作,但失败了。您可以在Rust Playground上尝试一下。即使使用RUST_BACKTRACE=full
,我也无法获得任何合理的细节来说明为什么失败了。
use std::path::Path;
fn main() {
let pwd = Path::new(r#"C:\Users\me"#);
let path = Path::new(r#"C:\Users\me\site"#);
let result = path.strip_prefix(pwd).map_err(|_| ());
println!("Result: {:?}", result); // Result: Err(())
path.strip_prefix(pwd).unwrap();
}
这是错误还是我错过了什么?
结果为RUST_BACKTRACE=full
:
Result: Err(())
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: StripPrefixError(())', src/libcore/result.rs:997:5
stack backtrace:
0: 0x55f339d5eb53 - std::sys::unix::backtrace::tracing::imp::unwind_backtrace::hf8722b0178fb1b63
at src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:39
1: 0x55f339d5aa68 - std::sys_common::backtrace::_print::hc40139e5f1d656ee
at src/libstd/sys_common/backtrace.rs:70
2: 0x55f339d5daa2 - std::panicking::default_hook::{{closure}}::h993d43465919c16a
at src/libstd/sys_common/backtrace.rs:58
at src/libstd/panicking.rs:200
3: 0x55f339d5d814 - std::panicking::default_hook::h73d2c2ec3d9ba5a4
at src/libstd/panicking.rs:215
4: 0x55f339d5e100 - std::panicking::rust_panic_with_hook::h744417edfe714d72
at src/libstd/panicking.rs:478
5: 0x55f339d5dc81 - std::panicking::continue_panic_fmt::h3557b3c3fa21b47b
at src/libstd/panicking.rs:385
6: 0x55f339d5db65 - rust_begin_unwind
at src/libstd/panicking.rs:312
7: 0x55f339d6e0cc - core::panicking::panic_fmt::h74ee8034b317ceed
at src/libcore/panicking.rs:85
8: 0x55f339d536fd - core::result::unwrap_failed::h3406097ad0bd8fc9
at /rustc/2aa4c46cfdd726e97360c2734835aa3515e8c858/src/libcore/macros.rs:16
9: 0x55f339d53429 - <core::result::Result<T, E>>::unwrap::hac51cf7638922ce6
at /rustc/2aa4c46cfdd726e97360c2734835aa3515e8c858/src/libcore/result.rs:798
10: 0x55f339d53e89 - g::main::h131de6fc3bc2b7fb
at src/main.rs:8
11: 0x55f339d538df - std::rt::lang_start::{{closure}}::h68e0b763dc36e392
at /rustc/2aa4c46cfdd726e97360c2734835aa3515e8c858/src/libstd/rt.rs:64
12: 0x55f339d5db52 - std::panicking::try::do_call::h7a0381557c6c2cee
at src/libstd/rt.rs:49
at src/libstd/panicking.rs:297
13: 0x55f339d5faa9 - __rust_maybe_catch_panic
at src/libpanic_unwind/lib.rs:92
14: 0x55f339d5e655 - std::rt::lang_start_internal::he0d8d06abc6f912f
at src/libstd/panicking.rs:276
at src/libstd/panic.rs:388
at src/libstd/rt.rs:48
15: 0x55f339d538b8 - std::rt::lang_start::h565ec575e9c57feb
at /rustc/2aa4c46cfdd726e97360c2734835aa3515e8c858/src/libstd/rt.rs:64
16: 0x55f339d53ec9 - main
17: 0x7f59a6820412 - __libc_start_main
18: 0x55f339d531fd - _start
19: 0x0 - <unknown>
答案 0 :(得分:1)
std::path::Path
的文档中说:
此类型支持许多检查路径的操作,包括将路径分为其各个组成部分(,在Unix上用
/
分隔,在Unix上用\
或\
分隔Windows ),提取文件名,确定路径是否为绝对路径,等等。
(强调是我的)
这意味着在Linux上,C:\Users\me
将不会被识别为路径分隔符,因此,C:\\Users\me\site
和\
只是当前目录中的文件名。在Linux上,$ mkdir foo
$ cd foo
$ touch 'C:\Users\me'
$ ls -l
total 0
-rw-r--r-- 1 martin martin 0 Apr 14 01:47 'C:\Users\me'
的确不是文件名中的特殊字符:
using System.Web.Management;
由于Playground在Linux上运行(as hinted by the fact that there is no deployment information for Windows,尽管从技术上讲也可以在Windows上托管它),所以您的代码无法在其中运行。
但是在Windows上,it works as expected。