简介
在本实验中,你将学习函数如何将闭包作为参数,从而允许任何满足闭包特征约束的函数用作参数。Fn
、FnMut
和 FnOnce
特征决定了闭包如何从封闭作用域中捕获变量。
注意:如果实验未指定文件名,你可以使用任何你想要的文件名。例如,你可以使用
main.rs
,并通过rustc main.rs &&./main
进行编译和运行。
Skills Graph
%%%%{init: {'theme':'neutral'}}%%%%
flowchart RL
rust(("Rust")) -.-> rust/BasicConceptsGroup(["Basic Concepts"])
rust(("Rust")) -.-> rust/FunctionsandClosuresGroup(["Functions and Closures"])
rust(("Rust")) -.-> rust/MemorySafetyandManagementGroup(["Memory Safety and Management"])
rust(("Rust")) -.-> rust/AdvancedTopicsGroup(["Advanced Topics"])
rust/BasicConceptsGroup -.-> rust/variable_declarations("Variable Declarations")
rust/FunctionsandClosuresGroup -.-> rust/function_syntax("Function Syntax")
rust/FunctionsandClosuresGroup -.-> rust/expressions_statements("Expressions and Statements")
rust/MemorySafetyandManagementGroup -.-> rust/lifetime_specifiers("Lifetime Specifiers")
rust/AdvancedTopicsGroup -.-> rust/operator_overloading("Traits for Operator Overloading")
subgraph Lab Skills
rust/variable_declarations -.-> lab-99326{{"作为函数参数的闭包"}}
rust/function_syntax -.-> lab-99326{{"作为函数参数的闭包"}}
rust/expressions_statements -.-> lab-99326{{"作为函数参数的闭包"}}
rust/lifetime_specifiers -.-> lab-99326{{"作为函数参数的闭包"}}
rust/operator_overloading -.-> lab-99326{{"作为函数参数的闭包"}}
end