学习 Rust 第 23 天:闭包

发布于:2024-05-01 ⋅ 阅读:(29) ⋅ 点赞:(0)

Rust 闭包提供了简洁、富有表现力的匿名函数来捕获周围的变量。它们简化了代码,提供了存储、参数传递和函数重构方面的灵活性。它们与泛型的交互增强了灵活性,而捕获模式则促进了有效的所有权和可变性管理。从本质上讲,闭包是 Rust 的基础,可以提高代码的清晰度和简洁性。

Introduction 介绍

We used a closure in themini_grep project, here’s what it looked like
我们在 mini_grep 项目中使用了闭包,如下所示

let config = Config::new(&args).unwrap_or_else(|err|{
        eprintln!("Problem parsing arguments: {}",err);
        eprintln!("Expected: {} search_query filename", args[0]);
        process::exit(1);
    });

Closures are like anonymous functions, and we can store them in variables as well, pass them as function arguments etc.
闭包就像匿名函数,我们也可以将它们存储在变量中,将它们作为函数参数传递等。

In Rust, closures are anonymous functions that can capture variables from their surrounding environment. They are similar to lambda functions or blocks in other programming languages. Closures in Rust have a flexible and powerful syntax, allowing them to be used in various contexts.
在 Rust 中,闭包是可以从周围环境捕获变量的匿名函数。它们类似于其他编程语言中的 lambda 函数或块。 Rust 中的闭包具有灵活而强大的语法,允许它们在各种上下文中使用。

fn main() {
    let add_one = |x| x + 1;

    let result = add_one(5);
    println!("Result: {}", result); // Output: 6
}

In this example, add_one is a closure that takes one parameter x and returns x + 1. It's assigned to a variable, and then called with an argument 5. The result is printed out.
在此示例中, add_one 是一个闭包,它采用一个参数 x 并返回 x + 1 。它被分配给一个变量,然后使用参数 5 进行调用。结果被打印出来。

This is a basic example of what a closure looks like.
这是闭包的基本示例。

Refactoring Functions to Closures
将函数重构为闭包

Refactoring functions with closures can often lead to more concise and expressive code.
使用闭包重构函数通常可以生成更简洁、更具表现力的代码。
Let’s consider a function that applies a given operation to each element in a vector:
让我们考虑一个将给定操作应用于向量中每个元素的函数:

fn apply_operation_to_vec(values: Vec<i32>, operation: fn(i32) -> i32) -> Vec<i32> {
    let mut result = Vec::new();
    for value in values {
        result.push(operation(value));
    }
    result
}

fn double(x: i32) -> i32 {
    x * 2
}

fn main() {
    let values = vec![1, 2, 3, 4, 5];
    let doubled_values = apply_operation_to_vec(values, double);
    println!("Doubled values: {:?}", doubled_values);
}

In this code, we have a function apply_operation_to_vec that takes a vector of integers and a function pointer operation representing the operation to be applied to each element of the vector.
在此代码中,我们有一个函数 apply_operation_to_vec ,它接受一个整数向量和一个表示要应用于向量每个元素的操作的函数指针 operation 。

Now, let’s refactor this function to use closures instead:
现在,让我们重构这个函数以使用闭包:

fn apply_operation_to_vec(values: Vec<i32>, operation: impl Fn(i32) -> i32) -> Vec<i32> {
    values.into_iter().map(operation).collect()
}

fn main() {
    let values = vec![1, 2, 3, 4, 5];
    let doubled_values = apply_operation_to_vec(values, |x| x * 2);
    println!("Doubled values: {:?}", doubled_values);
}

In this refactored version:
在这个重构版本中:

  • We’ve changed the operation parameter to accept a closure instead of a function pointer. The impl Fn(i32) -> i32 syntax means that operation can accept any type that implements the Fn(i32) -> i32 trait, which includes closures.
    我们更改了 operation 参数以接受闭包而不是函数指针。 impl Fn(i32) -> i32 语法意味着 operation 可以接受任何实现 Fn(i32) -> i32 特征的类型,其中包括闭包。
  • Inside the apply_operation_to_vec function, we use into_iter() to consume the input vector and produce an iterator. Then, we use map() to apply the closure to each element, and collect() to collect the results into a new vector.
    在 apply_operation_to_vec 函数内,我们使用 into_iter() 来使用输入向量并生成迭代器。然后,我们使用 map() 将闭包应用于每个元素,并使用 collect() 将结果收集到新的向量中。

Both output the same data:
两者输出相同的数据:

$ cargo run
Doubled values: [2, 4, 6, 8, 10]

Type annotations 类型注释

To declare an add_one function which returns an integer, and takes an integer as a parameter. The function declaration would look something like this
声明一个返回整数并接受整数作为参数的 add_one 函数。函数声明看起来像这样

fn add_one(x: i32)->i32{
  x+1
}

But If I want to do the same with closures, notice how I don’t need to specify any data types here…
但是如果我想对闭包做同样的事情,请注意我不需要在这里指定任何数据类型......

fn main(){
  let add_one = |x| x + 1;
}

The add_one closure is defined without specifying the types of its parameters and return value. Rust's type inference mechanism automatically deduces that add_one takes an i32 parameter and returns an i32 value.
add_one 闭包的定义没有指定其参数和返回值的类型。 Rust 的类型推断机制会自动推断 add_one 接受 i32 参数并返回 i32 值。

Generic parameters and Function traits
通用参数和函数特征

Fn trait:  Fn 功能:

  • Closures that implement Fn can be called immutably.
    实现 Fn 的闭包可以被不可变地调用。
  • They capture their environment by reference, allowing them to borrow variables from the surrounding scope.
    它们通过引用捕获环境,从而允许它们从周围范围借用变量。
  • This trait is suitable for closures that don’t need to mutate the captured variables.
    此特性适用于不需要改变捕获变量的闭包。
  • Example usage: Functions that only read from the captured variables.
    用法示例:仅读取捕获变量的函数。

FnMut trait:  FnMut 功能:

  • Closures that implement FnMut can be called mutably.
    实现 FnMut 的闭包可以被可变地调用。
  • They capture their environment by mutable reference, allowing them to modify the captured variables.
    他们通过可变引用捕获环境,从而允许他们修改捕获的变量。
  • This trait is suitable for closures that need to mutate the captured variables.
    此特性适用于需要改变捕获变量的闭包。
  • Example usage: Functions that modify the captured variables but don’t consume them.
    用法示例:修改捕获的变量但不消耗它们的函数。

FnOnce trait:  FnOnce 功能:

  • Closures that implement FnOnce take ownership of the captured variables.
    实现 FnOnce 的闭包拥有捕获的变量的所有权。
  • They can only be called once because they consume the captured variables.
    它们只能被调用一次,因为它们消耗捕获的变量。
  • This trait is suitable for closures that need to consume the captured variables, transferring ownership to the closure.
    此特性适用于需要使用捕获的变量、将所有权转移给闭包的闭包。
  • Example usage: Functions that consume the captured variables, such as closures used in move semantics.
    用法示例:使用捕获变量的函数,例如移动语义中使用的闭包。

In Rust, you can use generic parameters with function traits (FnFnMutFnOnce) to make functions more flexible and reusable across different types.
在 Rust 中,您可以将泛型参数与函数特征( Fn 、 FnMut 、 FnOnce )一起使用,以使函数在不同类型之间更加灵活和可重用。

// A generic function that takes a closure and applies it to an input value
fn apply_closure<F, T>(closure: F, value: T) -> T
where
    F: Fn(T) -> T,
{
    closure(value)
}

fn main() {
    // Define a closure that doubles an integer
    let double_closure = |x: i32| x * 2;

    // Apply the closure to a value
    let result = apply_closure(double_closure, 5);
    println!("Result: {}", result); // Output: Result: 10

    // Define a closure that appends a string
    let append_closure = |s: String| s + " World";

    // Apply the closure to a value
    let result = apply_closure(append_closure, String::from("Hello"));
    println!("Result: {}", result); // Output: Result: Hello World
}
  • We have a generic function apply_closure that takes two parameters: a closure (F) and a value (T). The closure must implement the Fn(T) -> T trait, meaning it takes a single parameter of type T and returns a value of type T.
    我们有一个通用函数 apply_closure ,它接受两个参数:一个闭包 ( F ) 和一个值 ( T )。闭包必须实现 Fn(T) -> T 特征,这意味着它采用 T 类型的单个参数并返回 T 类型的值。
  • Inside main(), we define two closures: double_closure and append_closure, each with different input and output types (i32 and String).
    在 main() 内部,我们定义了两个闭包: double_closure 和 append_closure ,每个闭包都有不同的输入和输出类型( i32 和 String
  • We then call apply_closure twice, passing each closure along with an appropriate value. The function applies the closure to the value and returns the result.
    然后我们调用 apply_closure 两次,传递每个闭包以及适当的值。该函数将闭包应用于该值并返回结果。

This approach allows us to use the same generic function with different closures, making our code more reusable and flexible. Additionally, by specifying the Fn trait bound, we ensure that the closures passed to apply_closure are callable and match the expected signature.
这种方法允许我们使用具有不同闭包的相同泛型函数,使我们的代码更加可重用和灵活。此外,通过指定 Fn 特征边界,我们确保传递给 apply_closure 的闭包是可调用的并且与预期签名匹配。

Capturing the environment with closures
用闭包捕捉环境

In Rust, closures can capture variables from their surrounding environment. This feature allows closures to access and manipulate variables that are defined outside of their own scope. Rust provides three ways for closures to capture variables: by reference (&T), by mutable reference (&mut T), or by value (T).
在 Rust 中,闭包可以从周围环境捕获变量。此功能允许闭包访问和操作在其自身范围之外定义的变量。 Rust 提供了三种闭包捕获变量的方法:通过引用 ( &T )、通过可变引用 ( &mut T ) 或通过值 ( T )。

Let's explore each method:
让我们探讨一下每种方法:

Capture by reference (&T):
通过引用捕获 ( &T ):

  • When a closure captures variables by reference, it borrows them immutably.
    当闭包通过引用捕获变量时,它会不可变地借用它们。
  • The closure can read but cannot modify the variables it captures.
    闭包可以读取但不能修改它捕获的变量。
  • The captured variables remain accessible and can be used after the closure’s execution.
    捕获的变量仍然可以访问,并且可以在闭包执行后使用。
  • This is the default behavior for closures that don’t explicitly specify how they capture variables.
    这是未明确指定如何捕获变量的闭包的默认行为。
fn main() {
    let x = 42;
    let closure = || {
        println!("Captured value: {}", x);
    };
    closure();
    // x is still accessible here
    println!("Outer value: {}", x);
}

Capture by mutable reference (&mut T):
通过可变引用捕获( &mut T ):

  • When a closure captures variables by mutable reference, it borrows them mutably.
    当闭包通过可变引用捕获变量时,它会可变地借用它们。
  • The closure can read and modify the variables it captures.
    闭包可以读取和修改它捕获的变量。
  • The captured variables remain mutable after the closure’s execution.
    捕获的变量在闭包执行后仍然可变。
  • To capture variables by mutable reference, the closure must be declared with the mut keyword.
    要通过可变引用捕获变量,必须使用 mut 关键字声明闭包。
fn main() {
    let mut x = 42;
    let mut closure = || {
        println!("Captured value before: {}", x);
        x += 1;
        println!("Captured value after: {}", x);
    };
    closure();
    // x is still accessible here, and its value has been modified by the closure
    println!("Outer value: {}", x);
}

Capture by value (T):
按值捕获 ( T ):

  • When a closure captures variables by value, it takes ownership of them.
    当闭包按值捕获变量时,它就获得了它们的所有权。
  • The closure can consume the variables it captures.
    闭包可以消耗它捕获的变量。
  • After the closure’s execution, the captured variables are moved into the closure and no longer accessible in the outer scope.
    闭包执行后,捕获的变量将移至闭包中,并且无法再在外部作用域中访问。
fn main() {
    let x = 42;
    let closure = move || {
        println!("Captured value: {}", x);
    };
    closure();
    // x is not accessible here; it has been moved into the closure
    // println!("Outer value: {}", x); // This line would cause a compilation error
}

网站公告

今日签到

点亮在社区的每一天
去签到