Post History
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 ...
#2: Post edited
- ## 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
## 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.