Tips for golfing in Rust

26

2

While Rust is very, very rarely competitive in code golfing competitions (Java is often shorter), it can still be fun to golf in. What are some tricks for making Rust code shorter?

Please post only one tip in each answer.

Doorknob

Posted 2016-02-24T21:22:11.790

Reputation: 68 138

Answers

13

Use closures instead of functions

A closure:

|n|n+1

is shorter than a function:

fn f(n:i32)->i32{n+1}

Closures longer than one statement need braces but are still far shorter than a function.

Doorknob

Posted 2016-02-24T21:22:11.790

Reputation: 68 138

10

Avoid .iter().enumerate()

Let's say you have some x that implements the IntoIterator Trait and you need to call a function f that takes the index of an element and a reference to it. The standard way of doing this is

x.iter().enumerate().map(f)

instead you can do

(0..).zip(x).map(f)

and save yourself not only the unusually long enumerate but also the call to iter!

raggy

Posted 2016-02-24T21:22:11.790

Reputation: 491

9

If you need many mutable variables, it can waste a lot of space declaring them and initializing them, since each requires the mut keyword and you can't do anything like a=b=c=0. A solution then is to declare a mutable array

let mut a=[0;5];

You spend 3 extra bytes each time you use them vs. a normal variable:

a[0]+=1;

but it can often still be worth it.

Using tuples for the same trick is often an even better option:

let mut t=(1,4,"this", 0.5, 'c');

This has the advantage of saving a byte on each use vs. the array:

t.0=2

It also lets them be of different types. On the downside, it requires more characters to initialize.

Harald Korneliussen

Posted 2016-02-24T21:22:11.790

Reputation: 430

8

Converting &str to String

Never do these:

s.to_string()  // 13 bytes
s.to_owned()   // 12 bytes

This is always shorter:

s.repeat(1)    // 11 bytes

If s is a string literal:

format!(s)     // 10 bytes

For example: use format!("") instead of String::new() to save 2 bytes.

If type inference works:

s.into()       // 8 bytes

JayXon

Posted 2016-02-24T21:22:11.790

Reputation: 159

6

When using string formatting for example with print!(), one can use both numbered and unnumbered formatters to save one byte per item to format:

Best shown with an example:

fn main(){
    print!("{}{}{}. Yes, {0}{}{2}. All you other{1}{2}s are just imitating.",
           "I'm",
           " Slim",
           " Shady",
           " the real");
}

Which outputs:

I'm Slim Shady. Yes, I'm the real Shady. All you other Slim Shadys are just imitating.

So the unnumbered formatters will get assigned to the items in order, this allows you to skip the index on them. Note that you can only use one unnumbered formatter per item to format, after that it will get used up.

ruohola

Posted 2016-02-24T21:22:11.790

Reputation: 414

5

Reading lines

After considering various things, I think that is generally the shortest way to retrieve a line. The line has a newline, it can be removed by trimming (.trim()) or if that cannot be done by slicing.

let y=&mut"".into();std::io::stdin().read_line(y);

For multiple lines, lines iterator can be used, the iterated line doesn't end in newline then. A glob import is needed to import BufRead, needed for lines to be available for StdinLock<'_> type.

use std::io::*;let y=stdin();y.lock().lines()

Konrad Borowski

Posted 2016-02-24T21:22:11.790

Reputation: 11 185

4

Skipping trailing semicolons

In functions returning (), where the last expression is also of type (), you don't need the trailing ;:

fn main(){print!("Hello, world!")}

NieDzejkob

Posted 2016-02-24T21:22:11.790

Reputation: 4 630

4

When working with strings with newlines in them, you save one byte if you use a literal line break in the source code vs having a \n in the string.

println!("Hello
World!");

is 1 byte less than:

println!("Hello\nWorld!);

ruohola

Posted 2016-02-24T21:22:11.790

Reputation: 414

3

When using whole number floating point numbers, you can omit the trailing .0 to save one byte.

let a=1.

is 1 byte less than:

let a=1.0

ruohola

Posted 2016-02-24T21:22:11.790

Reputation: 414

0

Use closure parameter to define variable instead of let

{let(a,b)=(A,B);X}

can be replaced by

(|a,b|X)(A,B)

to save 5 bytes if the type of a and b can be inferred, it usually works when they are integers or are passed to another function directly in X.

This works well on 2 or more variables, but for a single variable, this could still save 2 bytes if it allows you to get rid of the {}.

JayXon

Posted 2016-02-24T21:22:11.790

Reputation: 159

0

Use ..=

Replace all ..i+1 with ..=i to save 1 byte, works on both array index a[i..=i] and range (0..=n)

JayXon

Posted 2016-02-24T21:22:11.790

Reputation: 159

clippy actually has a warning for this – betseg – 2020-02-26T05:20:16.300