相关疑难解决方法(0)

如何匹配表示为OsStr的文件扩展名?

我试图match反对文件扩展名:

let file_path = std::path::Path::new("index.html");
let content_type = match file_path.extension() {
    None => "",
    Some(os_str) => match os_str {
        "html" => "text/html",
        "css" => "text/css",
        "js" => "application/javascript",
    },
};
Run Code Online (Sandbox Code Playgroud)

编译器说:

error[E0308]: mismatched types
 --> src/main.rs:6:13
  |
6 |             "html" => "text/html",
  |             ^^^^^^ expected struct `std::ffi::OsStr`, found str
  |
  = note: expected type `&std::ffi::OsStr`
             found type `&'static str`
Run Code Online (Sandbox Code Playgroud)

string rust

4
推荐指数
2
解决办法
1662
查看次数

标签 统计

rust ×1

string ×1