windows - 剥离 Windows 路径前缀

标签 windows path rust

我正在尝试从 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>

最佳答案

作为std::path::Path的文档说:

This type supports a number of operations for inspecting a path, including breaking the path into its components (separated by / on Unix and by either / or \ on Windows), extracting the file name, determining whether the path is absolute, and so on.

(重点是我的)

这意味着在 Linux 上,\ 不会被识别为路径分隔符,因此,C:\Users\meC:\\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'

由于 Playground 在 Linux 上运行(as hinted by the fact that there is no deployment information for Windows,尽管技术上也可以在 Windows 上托管它),您的代码无法在其中运行。

但是在 Windows 上,it works as expected .

关于windows - 剥离 Windows 路径前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55665098/

相关文章:

windows - 如何等待进程终止以在批处理文件中执行另一个进程

python - 如何在 Windows 上使用 Popen 隐藏控制台?

c++ - 在 Windows 上检查非提升用户的文件权限

bash - 使用 bash tab-completion 忽略路径条目

rust - 当比较依赖于数据而不是被比较项目的一部分时,如何实现 Ord?

asynchronous - 如何转换 future 的输出?

php - imagick 崩溃与 PHP 5.3

algorithm - 如何使用WordNet路径算法计算两个字符串中单词的语义相似度

Linux 导出命令语法

generics - 在 Rust 库中仅公开通用私有(private)类型的具体变体