我試圖列出目錄中的文件,然後將文件名複製到我自己的Vec中。我嘗試了幾種解決方案,但總是以無法創建足夠長的生活變量爲題。我不明白我的錯誤。創建Vec時借用的值不夠長住
fn getList(action_dir_path : &str) -> Vec<&str> {
let v = fs::readdir(&Path::new(action_dir_path))
.unwrap()
.iter()
.map(|&x| x.filestem_str().unwrap())
.collect();
return v;
}
我想知道爲什麼編譯器會抱怨「x」?我不在乎x,我想要它裏面的& str,我認爲& str是靜態的。
我試過這種方式,但是我得到了與抱怨「路徑」不夠長的編譯器相同的結果。
fn getList2(action_dir_path : &str) -> Vec<&str> {
let paths = fs::readdir(&Path::new(action_dir_path)).unwrap();
let mut v : Vec<&str> = Vec::new();
for path in paths.iter(){
let aSlice = path.filestem_str().unwrap();
v.push(aSlice);
}
return v;
}
這裏的圍欄:http://is.gd/4FajDs
謝謝你的回答,我現在明白了! thx也爲您的免費更正和建議! – mrburne 2015-02-05 22:38:40