英文:
I don't understand this Rust closure syntax
问题
I am currently reading the source code of dua-cli and came across a function call that requires an argument satisfying the Fn trait. The code is:
type WalkDir = jwalk::WalkDirGeneric<((), Option<Result<std::fs::Metadata, jwalk::Error>>)>;
impl WalkOptions {
pub(crate) fn iter_from_path(&self, root: &Path, root_device_id: u64) -> WalkDir {
WalkDir::new(root)
.follow_links(false)
.sort(match self.sorting {
TraversalSorting::None => false,
TraversalSorting::AlphabeticalByFileName => true,
})
.skip_hidden(false)
.process_read_dir({
// I don't understand the following two lines
let ignore_dirs = self.ignore_dirs.clone();
let cross_filesystems = self.cross_filesystems;
move |_, _, _, dir_entry_results| {
// Closure logic here
}
})
}
}
The process_read_dir function signature is:
pub fn process_read_dir<F>(self, process_by: F) -> Selfwhere
F: Fn(Option<usize>, &Path, &mut C::ReadDirState, &mut Vec<Result<DirEntry<C>>>) + Send + Sync + 'static,
In the above code, the function call doesn't directly pass a closure to process_read_dir. Instead, it begins with the declaration of two local variables enclosed in curly braces, followed by a closure move |_, _, _, dir_entry_results|
. I'm wondering why this code is considered valid for the function call. I mean if it only passes the closure as an argument, I am fine, but I am confused with the local variable declaration in the front and the outside curly braces.
Could you please explain?
英文:
I am currently reading the source code of dua-cli and came across a function call that requires an argument satisfying the Fn trait. The code is:
type WalkDir = jwalk::WalkDirGeneric<((), Option<Result<std::fs::Metadata, jwalk::Error>>)>;
impl WalkOptions {
pub(crate) fn iter_from_path(&self, root: &Path, root_device_id: u64) -> WalkDir {
WalkDir::new(root)
.follow_links(false)
.sort(match self.sorting {
TraversalSorting::None => false,
TraversalSorting::AlphabeticalByFileName => true,
})
.skip_hidden(false)
.process_read_dir({
// I don't understand the following two lines
let ignore_dirs = self.ignore_dirs.clone();
let cross_filesystems = self.cross_filesystems;
move |_, _, _, dir_entry_results| {
// Closure logic here
}
})
}
}
The process_read_dir function signature is
pub fn process_read_dir<F>(self, process_by: F) -> Selfwhere
F: Fn(Option<usize>, &Path, &mut C::ReadDirState, &mut Vec<Result<DirEntry<C>>>) + Send + Sync + 'static,
In the above code, the function call doesn't directly pass a closure to process_read_dir. Instead, it begins with the declaration of two local variables enclosed in curly braces, followed by a closure move |_, _, _, dir_entry_results|
. I'm wondering why this code is considered valid for the function call. I mean if it only pass the closure as argument, I am fine, but I am confused with the local variable declaration in the front and the outside curly braces.
Could you please explain?
答案1
得分: 9
The code you provided appears to be written in Rust. Here's the translated portion:
{
let ignore_dirs = self.ignore_dirs.clone();
let cross_filesystems = self.cross_filesystems;
move |_, _, _, dir_entry_results| {
// 闭包逻辑在这里
}
}
Please note that the code includes a closure (闭包) with some logic inside it. If you have any specific questions or need further assistance with this code, please feel free to ask.
英文:
{
let ignore_dirs = self.ignore_dirs.clone();
let cross_filesystems = self.cross_filesystems;
move |_, _, _, dir_entry_results| {
// Closure logic here
}
}
is just a regular block expression, like all blocks in Rust it evaluates to the last expression in it in this case the closure.
The two assignments are simply assignments, they do what all let
bindings do, not sure where you're confused there either.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论