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

Rust golfing tips

+1
−0

What tips do you have for saving bytes in Rust?

Please post one tip per answer for ease of voting and commenting.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

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.

+1
−0

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

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

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


  1. 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. ↩︎

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

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

Test both on Rust Playground


  1. 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 returns None for an empty input. ↩︎

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »