1
我知道有一個類似的任務,但它不是特定於Windows的,並且可能不是最有效的方法。我也希望學習如何實現我已經知道如何在C#中使用Rust。有效的方法來獲取硬盤驅動器的所有邏輯驅動器號並收集根目錄
這是我到目前爲止已經試過:read_dir source(更新)
fn main() {
unsafe{
for mut cur_drl in get_win32_ready_drives(){
let mut mcur_drl = cur_drl.clone();
let mut cur_path = PathBuf::from(mcur_drl);
//here i try to print root of current logical
let unwrpd_curfspath = fs::metadata(cur_path).unwrap();
if unwrpd_curfspath.is_dir(){
for entry in (fs::read_dir(cur_path)) {
let curfspath2 = fs::metadata(entry.path()).unwrap();
if curfspath2.is_dir(){
cur_drl.push_str(entry.path());
println!("drive {0}", cur_drl);
}
}
}
}
}
}
// this is the working part of the project so far
// getting all drive letters
//(hdd only actually (3 is hdd & 2 = removable..sd cards etc') into a list
unsafe fn get_win32_ready_drives() -> Vec<String>
{
let mut logical_drives = Vec::with_capacity(5);
let mut bitfield =kernel32::GetLogicalDrives();
let mut drive = 'A';
let mut rtstr = CString::new("");
while bitfield != 0 {
if bitfield & 1 == 1 {
let strfulldl = drive.to_string() + ":\\";
let cstrfulldl = CString::new(strfulldl.clone()).unwrap();
let drvT = kernel32::GetDriveTypeA(cstrfulldl.as_ptr());
if(drvT ==3 || drvT ==2)
{
logical_drives.push(strfulldl);
// println!("drive {0} is {1}", strfdl, drvT);
}
}
drive = std::char::from_u32((drive as u32) + 1).unwrap();
bitfield >>= 1;
}
logical_drives
}
此代碼不能編譯,因爲我有問題 瞭解每一個返回類型,以及如何妥善處理迭代其結果。
@Shepmaster感謝。和'read_dir'源來自http://web.mit.edu/rust-lang_v1.2/arch/amd64_ubuntu1404/share/doc/rust/html/std/fs/fn.read_dir.html –
我鼓勵你要[編輯]你的問題來添加該鏈接。我還會添加一個鏈接到您提到的箱子,以幫助未來的人解決他們的問題,而無需爲了學習目的而重寫代碼。另外,該文檔鏈接到Rust 1.2,而1.4是當前的穩定版本。 – Shepmaster
此外,你的問題是與單獨的問題混淆。你似乎在問一個具體的問題,需要編譯。您應該減少代碼以生成專注於該特定問題的[MCVE](/ help/mcve)。一旦你有工作代碼,你可能想問[代碼評論](http://meta.codereview.stackexchange.com/questions/5777/a-guide-to-code-review-for-stack-overflow-users )提供更高效的反饋意見。 – Shepmaster