Rust 什么';这是从&;转换的正确方法;str到OsStr?
我正在使用,它将装载选项作为Rust 什么';这是从&;转换的正确方法;str到OsStr?,rust,Rust,我正在使用,它将装载选项作为&[&std::ffi::os_str::OsStr]。似乎我应该拆分传入的逗号分隔选项字符串,我这样做: mod fuse { use std::ffi::OsStr; pub fn mount(options: &[&OsStr]) {} } fn example(optstr: &str) { let mut options: &[&str] = &[]; if optstr !
&[&std::ffi::os_str::OsStr]
。似乎我应该拆分传入的逗号分隔选项字符串,我这样做:
mod fuse {
use std::ffi::OsStr;
pub fn mount(options: &[&OsStr]) {}
}
fn example(optstr: &str) {
let mut options: &[&str] = &[];
if optstr != "" {
options = optstr.split(",").collect::<Vec<_>>().as_slice();
}
fuse::mount(options)
}
我的印象是所有的&str
s也都是OsStr
s,但我是个新手,所以我猜这是错的 使用:
请注意,显式类型规范不是必需的,我只是出于教育目的将其包括在内
在您的具体情况下:
let options: Vec<_> = optstr.split(",").map(OsStr::new).collect();
fuse::mount(&options)
let选项:Vec=optstr.split(“,”).map(OsStr::new.collect();
保险丝::安装(&选项)
然而,实际上很少需要明确地这样做。大多数情况下,函数接受实现
AsRef
的类型。这将允许您无需考虑就可以传递更多类型。你可能想考虑维护者或者向库提交补丁以使它更通用。< / P > @ BogdanMart,这个代码在Windows 10上用RISUD1.42.0编译好。对此我真的很抱歉。在当时与编译器斗争之后,我有点困惑。当我意识到自己错了时,堆栈溢出不允许我收回我的否决票。但我已经要求我的朋友投票来补偿我的失败。再次抱歉。
let options: Vec<_> = optstr.split(",").map(OsStr::new).collect();
fuse::mount(&options)