Rust Cheatsheet ![](https://camo.githubusercontent.com/3213f17517405a0763d09df131a8aa7c3fc19534d048832f970c23e0ccdba1ad/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f7468756d622f322f32302f52757374616365616e2d6f7269672d6e6f736861646f772e7376672f3132383070782d52757374616365616e2d6f7269672d6e6f736861646f772e7376672e706e67)
cargo new <project-name>
cargo run
- exe file is generated in
./target/debug
cargo build
fn main() {
println!("Hello, world!");
}
// automatic type
let var = "String";
// set mutability
let mut var = <value>;
// set type
let var: <type> = <value>;
/*
Type:
bool = true, false
i, 16, 32, 64 = number in range of x bits, can be negative
u, 16, 32, 64 = number in range of x bits, can't be negative
f, 64 = decimal numbers
String = string
*/
// automatic type
let t = (20, 10, 30);
// set type
let t: (i32, i32, i32) = (20, 10, 30);
let (x, y, z)= t;
t.0 // 20
x // 20
// automatic type
let a = [20, 10, 30];
// set type
let a: [i32, 3] = [20, 10, 30];
a[0] // 20
struct Name {
value1: <type>,
value2: <type>,
}
impl Name {
fn add(&self) -> i32 {
self.value1 + self.value2
}
}
let x = Name {
value1 = 5,
value2 = 10,
}
x.add() // 15
fn name() {
//...
}
// return
fn name() -> <type> { return x }
// parameters
fn name(param1: <type>) { }
if x {
// ...
} else if {
// ...
} else {
// ...
}
for i in 0..10 {
// ...
}
for x in y {
// ...
}
while x {
// ...
}