📰 IT NachrichtenLinkdump 37/2026(11.09.2026 um 11:42 Uhr)
📰 IT NachrichtenDie große Ratlosigkeit – Wie geht es weiter in der CDU?(11.09.2026 um 13:29 Uhr)
📰 IT NachrichtenWüst und die CDU im Wind der Brandmauer-Debatte(11.09.2026 um 14:47 Uhr)
🔧 AI Nachrichten KI-Angriffe: Geheimdienste sollen neue Befugnisse erhalten(11.09.2026 um 11:00 Uhr)
📰 IT NachrichtenKI-Hack: Anthropic muss nächsten Vorfall mit Claude offenlegen(11.09.2026 um 16:00 Uhr)
🔧 AI Nachrichten Podcast: ChatGPT schwatzt Nutzern in Deutschland jetzt Werbung auf(28.08.2026 um 08:46 Uhr)
🐧 Linux TippsPACMAN: KI-Framework steuert Fusionsplasma in Echtzeit(11.09.2026 um 10:46 Uhr)
📰 IT NachrichtenLinkdump 37/2026(11.09.2026 um 11:42 Uhr)
📰 IT NachrichtenDie große Ratlosigkeit – Wie geht es weiter in der CDU?(11.09.2026 um 13:29 Uhr)
📰 IT NachrichtenWüst und die CDU im Wind der Brandmauer-Debatte(11.09.2026 um 14:47 Uhr)
🔧 AI Nachrichten KI-Angriffe: Geheimdienste sollen neue Befugnisse erhalten(11.09.2026 um 11:00 Uhr)
📰 IT NachrichtenKI-Hack: Anthropic muss nächsten Vorfall mit Claude offenlegen(11.09.2026 um 16:00 Uhr)
🔧 AI Nachrichten Podcast: ChatGPT schwatzt Nutzern in Deutschland jetzt Werbung auf(28.08.2026 um 08:46 Uhr)
🐧 Linux TippsPACMAN: KI-Framework steuert Fusionsplasma in Echtzeit(11.09.2026 um 10:46 Uhr)

🔧 Programmierung 🕛 vor 2 Monaten 3 Min Lesezeit
0

[Rust Guide] 13.4. Capturing the Environment With Closures

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




13.4.0 Before We Begin



During its design, Rust drew inspiration from many languages, and functional programming had a particularly strong influence on Rust. Functional programming often includes passing functions as values to parameters, returning them from other functions, assigning them to variables for later execution, and so on.



In this chapter, we will discuss some Rust features that are similar to what many languages call functional features:




  • Closures (this article)

  • Iterators

  • Improving the I/O Project with Closures and Iterators

  • Performance of Closures and Iterators



If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series.






13.4.1 Closures Can Capture Their Environment



Closures have a capability that functions do not: a closure can access variables in the scope where it is defined.



Take a look at an example:




CODE
fn main() {
let x = 4;
let equal_to_x = |z| z == x;
let y = 4;
assert!(equal_to_x(y));
}






The closure part is:




CODE
let equal_to_x = |z| z == x;






Some people may find it hard to distinguish the roles of = and == here, so let’s rewrite it another way:




CODE
let equal_to_x = |z| {
z == x;
}






In other words, the closure takes z as its parameter, compares it with x (which is 4, because x = 4 was defined above), and returns a boolean. If they are equal, the result is true; otherwise it is false.



Here the closure directly accesses the variable x in the same scope, which functions cannot do.



But this feature has a cost: it introduces memory overhead. In most cases we do not need a closure to capture its environment, and we do not want the extra overhead either. That is why functions are not allowed to capture variables from the environment, and defining and using a function never introduces this kind of overhead.






13.4.2 How Closures Capture Values From Their Environment



Closures capture values from the environment in three ways, just like functions receive parameters in three ways:




  • Taking ownership, whose trait is FnOnce because a closure cannot take and consume the same variable more than once, so it can only be called once.

  • Mutable borrowing, whose trait is FnMut

  • Immutable borrowing, whose trait is Fn



When a programmer creates a closure, Rust infers which trait should be used based on how the closure uses values from the environment:




  • All closures implement FnOnce, because every closure can be called at least once

  • Closures that do not move captured variables implement FnMut

  • Closures that do not need mutable access to captured variables implement Fn



In fact, these three have an inclusion relationship: every Fn also implements FnMut, and every FnMut also implements FnOnce.






13.4.3 The move Keyword



Using the move keyword before the parameter list forces a closure to take ownership of the environment values it uses. This is most useful when passing a closure to a new thread and moving data so that it belongs to that new thread.



Take a look at an example:




CODE
fn main() {
let x = vec![1, 2, 3];
let equal_to_x = move |z| z == x;
println!("can't use x here {:?}", x);
let y = vec![1, 2, 3];
assert!(equal_to_x(y));
}






After using move, ownership of x moves into the closure, so x can no longer be used afterward.






13.4.4 Best Practice



When you specify one of the Fn trait bounds, start with Fn. Depending on what happens inside the closure, the compiler will tell you if FnOnce or FnMut is needed instead.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
Use custom web fonts in Google Sheets charts
2 Quellen
Introducing the new 1Password App for Google Chat
1 Quelle
Context-aware access controls are available for Gemini Enterprise in the Admin console
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten [Rust Guide] 13.4. Capturing the Environment With Closures

Thematisch verwandte Begriffe: Rust, Guide, Capturing, Environment · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...