Wiki: Rust
GitHub: edit | view

See Tips for golfing in Rust on CGSE.

Program structure examples

Print all the args:

fn main(){for v in std::env::args().skip(1){print!("{v}

Print a bunch of numbers:

fn main(){for i in 1..101{if i%9>6{print!("{}

Parse input:

// Ordinal Numbers
fn main(){for a in std::env::args(){for n in a.parse::<usize>(){ /* code */ }}}

// Fractions
fn main(){for a in std::env::args(){if let[x,y]=a.split("/").flat_map(str::parse).collect::<Vec<u32>>()[..]{ /* code */ }}}

By using for or if let we avoid the need for both .unwrap() and args().skip(1).

Stateful iteration:

fn main(){let mut k=1;for i in 0..10{print!("{k}␊");k*=3}}
fn main(){(0..10).fold(1,|k,_|{print!("{k}␊");k*3});}
fn main(){f(1)}fn f(k:i16){print!("{k}␊");f(k*3)}          // uses i16 overflow to terminate!

Random tips