Professional Documents
Culture Documents
Go, Rust Cheat Sheet 1
Go, Rust Cheat Sheet 1
Programming-Idioms.org
Creative Commons Attribution-ShareAlike 3.0
Go Rust
1 Print Hello World
Print a literal string on fmt.Println("Hello World") println!("Hello World");
standard output
2 Print Hello 10 times
Loop to execute some for i := 0; i < 10; i++ { for _ in 0..10 { println!("Hello"); }
code a constant number fmt.Println("Hello")
of times
}
Alternative implementation:
print!("{}", "Hello\n".repeat(10));
Alternative implementation:
fmt.Println(strings.Repeat("Hello\n", 1
0))
3 Create a procedure
Like a function which func finish(name string) { fn finish(name: &str) {
doesn't return any value,
fmt.Println("My job here is done. Goo println!("My job here is done. Good
thus has only side
d bye " + name) bye {}", name);
effects (e.g. Print to } }
standard output)
Alternative implementation:
4 Create a function
func square(x int) int { fn square(x : u32) -> u32 { x * x }
Create a function which
return x*x
returns the square of an
}
integer
Alternative implementation:
items.into_iter().for_each(|x| do_somet
hing(x));
Alternative implementation:
items.iter().enumerate().for_each(|(i,
x)| {
println!("Item {} = {}", i, x);
})