我不理解这个Rust闭包语法。

huangapple go评论72阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月15日 05:55:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定