야간에 녹 1.15를 사용 중이며 std::io::Result<std::fs::DirEntry>
이상의 코드가 있습니다. 각각의 DirEntry
이 파일 또는 폴더에 해당하는지 여부에 따라 파티션을 지정하려고합니다 (이름은 dir_contents_iterator
)결과를 사용할 때 예상 결과 및 결과
let (dir_list, file_list): (Vec<std::io::Result<DirEntry>>, Vec<std::io::Result<DirEntry>>) =
dir_contents_iterator.partition(|entry_result| {
match entry_result {
Ok(entry) => {
entry
.file_type()
.and_then(|file_type| Ok(file_type.is_dir()))
.unwrap_or(false)
},
Err(_) => false
}
});
이 코드는 다음과 같은 두 가지 오류와 함께 컴파일에 실패 :
error[E0308]: mismatched types
--> src/main.rs:179:13
|
179 | Ok(entry) => {
| ^^^^^^^^^ expected reference, found enum `std::result::Result`
|
= note: expected type `&std::result::Result<std::fs::DirEntry, std::io::Error>`
= note: found type `std::result::Result<_, _>`
error[E0308]: mismatched types
--> src/main.rs:185:13
|
185 | Err(_) => false
| ^^^^^^ expected reference, found enum `std::result::Result`
|
= note: expected type `&std::result::Result<std::fs::DirEntry, std::io::Error>`
= note: found type `std::result::Result<_, _>`
내 대구를 다시 작성하여 내 문제를 해결 결국 파일 목록에 분류 Err
값)와 e 파일과 폴더를 분할하기 전에 먼저 Err
값을 분할합니다. 이렇게하면 표시된 코드에서 일치 항목을 제거하고 컴파일 할 수 있습니다. 그러나 이해를 돕기 위해, 원래 제 시합의 브랜치 팔이 Result
을 받아들이 기보다는 &Result
참조를 기대하는지, 특히 값을 사용하지 않는 두 번째 브랜치 팔 (Err(_) => false
)의 경우를 알고 싶습니다. !
Here's the problem code in the context of the project I'm working on 문제를 설명하는 데 도움이되는 세부 정보를 빠뜨린 경우를 대비하여 (귀하가 제공 한한다)를 MCVE으로 이것을 사용
당신은 방금 일치 항목에서'* entry_result'를 사용하고 나서 match arm에서'ref entry'를 사용하여 마음을 아프게했습니다. 나는 그런 식으로 일한다는 것을 전혀 몰랐습니다. 나는'*'와'ref '가 서로를 원상태로 돌릴 것이라는 인상을 받았다. – user3791276
@ user3791276 님이 1 비트를 더 추가했습니다. – Shepmaster