Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

50%
+0 −0
Q&A Rust golfing tips

Avoid assignment & semicolon of unnamed closure Closure (15 bytes) If you need to call a closure using it's name (such as if it is recursive) then you need the bytes required to assign it to ...

posted 8d ago by trichoplax‭  ·  edited 8d ago by trichoplax‭

Answer
#2: Post edited by user avatar trichoplax‭ · 2025-04-10T23:41:21Z (8 days ago)
Typo
  • ## Avoid assignment & semicolon of unnamed closure
  • ### Closure (15 bytes)
  • If you need to call a closure using it's name (such as if it is recursive) then you need the bytes required to assign it to a variable, including a trailing semicolon to end the assignment.
  • ```rust
  • let f=|a,b|a*b;
  • ```
  • [Test named closure on Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=50791b04366d0a380af84b3b483affb4)
  • ### Anonymous closure (8 bytes)
  • If you never name the closure, you don't need the bytes for assigning it to a variable[^1], and therefore you also don't need the trailing semicolon, saving a total of 7 bytes:
  • ```rust
  • |a,b|a*b
  • ```
  • Here is that same anonymous closure being used without being named (as the second argument to `fold`), showing that it is a fully formed function and therefore valid for use as an answer without the need for the semicolon:
  • ```rust
  • println!("{}",(1..6).fold(1,|a,b|a*b));
  • ```
  • [Test named closure on Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=0a5f361263bee55f2e51c2d0cdf33288)
  • [^1]: This approach is dependent on the [general policy for languages which allow anonymous functions](https://codegolf.codidact.com/posts/292347/292350#answer-292350), which is in need of voting before it can be considered consensus. If you have an opinion, please vote up or down on that Meta answer, or comment or add alternative Meta answers.
  • ## Avoid assignment & semicolon of unnamed closure
  • ### Closure (15 bytes)
  • If you need to call a closure using it's name (such as if it is recursive) then you need the bytes required to assign it to a variable, including a trailing semicolon to end the assignment.
  • ```rust
  • let f=|a,b|a*b;
  • ```
  • [Test named closure on Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=50791b04366d0a380af84b3b483affb4)
  • ### Anonymous closure (8 bytes)
  • If you never name the closure, you don't need the bytes for assigning it to a variable[^1], and therefore you also don't need the trailing semicolon, saving a total of 7 bytes:
  • ```rust
  • |a,b|a*b
  • ```
  • Here is that same anonymous closure being used without being named (as the second argument to `fold`), showing that it is a fully formed function and therefore valid for use as an answer without the need for the semicolon:
  • ```rust
  • println!("{}",(1..6).fold(1,|a,b|a*b));
  • ```
  • [Test unnamed closure on Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=0a5f361263bee55f2e51c2d0cdf33288)
  • [^1]: This approach is dependent on the [general policy for languages which allow anonymous functions](https://codegolf.codidact.com/posts/292347/292350#answer-292350), which is in need of voting before it can be considered consensus. If you have an opinion, please vote up or down on that Meta answer, or comment or add alternative Meta answers.
#1: Initial revision by user avatar trichoplax‭ · 2025-04-10T21:11:17Z (8 days ago)
## Avoid assignment & semicolon of unnamed closure

### Closure (15 bytes)

If you need to call a closure using it's name (such as if it is recursive) then you need the bytes required to assign it to a variable, including a trailing semicolon to end the assignment.

```rust
let f=|a,b|a*b;
```

[Test named closure on Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=50791b04366d0a380af84b3b483affb4)

### Anonymous closure (8 bytes)

If you never name the closure, you don't need the bytes for assigning it to a variable[^1], and therefore you also don't need the trailing semicolon, saving a total of 7 bytes:

```rust
|a,b|a*b
```

Here is that same anonymous closure being used without being named (as the second argument to `fold`), showing that it is a fully formed function and therefore valid for use as an answer without the need for the semicolon:

```rust
println!("{}",(1..6).fold(1,|a,b|a*b));
```

[Test named closure on Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=0a5f361263bee55f2e51c2d0cdf33288)

[^1]: This approach is dependent on the [general policy for languages which allow anonymous functions](https://codegolf.codidact.com/posts/292347/292350#answer-292350), which is in need of voting before it can be considered consensus. If you have an opinion, please vote up or down on that Meta answer, or comment or add alternative Meta answers.