Rust golfing tips
What tips do you have for saving bytes in Rust?
Please post one tip per answer for ease of voting and commenting.
Avoid type annotations with a …
8d ago
Replace `reduce` with `fold` …
8d ago
Avoid assignment & semicolon o …
8d ago
3 answers
You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.
Avoid type annotations with a closure instead of a fn
Function (24 bytes)
A function requires types to be specified for its return value, and for any arguments it takes.
fn f(a:u8,b:u8)->u8{a*b}
Test function on Rust Playground
Closure (15 bytes)
A closure does not generally require types to be specified for either its return value or any arguments it takes.
let f=|a,b|a*b;
Test closure on Rust Playground
0 comment threads
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.
let f=|a,b|a*b;
Test named closure on Rust Playground
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:
|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:
println!("{}",(1..6).fold(1,|a,b|a*b));
Test unnamed closure on Rust Playground
-
This approach is dependent on the general policy for languages which allow anonymous functions, 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. ↩︎
0 comment threads
Replace reduce
with fold
Unlike some languages, Rust's reduce returns an Option
, requiring additional bytes to unwrap
the output. However, Rust's fold does not.
If you can find a suitable neutral initial value that will keep your output the same (such as the additive identity 0
for a sum, or the multiplicative identity 1
for a product, or the empty string ""
for string concatenation), then you can use fold
instead of reduce
. This does not need the output to be in an Option
because supplying an initial value means there is no doubt over what the output should be for an empty input[1], so fold
returns the output directly.
println!("{}",(1..6).reduce(|a,b|a*b).unwrap()); // 120
println!("{}",(1..6).fold(1,|a,b|a*b)); // 120
-
The output that makes sense for an empty input will differ depending on the operation being carried out. The sum of an empty list of numbers is 0, but the product of an empty list of numbers is 1. Since
reduce
accepts an arbitrary function as the operation, and Rust cannot know what the appropriate output for an empty input would be for all functions, it returnsNone
for an empty input. ↩︎
0 comment threads