Table of contents
rust
packages
use (import external function)
extern crate rand;
use std::thread
import multiple functions at once
use std::{thread, time}
modules
create an additional module (file) and call a function in it
main.rs:
mod other;
fn main() {
other::run();
}
other.rs:
pub fn run() {
println!("hello from other");
}
syntax
comments
line comment
// ...
block comment
/*
...
*/
markdown comment
///
variables
declare a constant
let mynumber = 8;
declare mutable variable
let mut count = 0;
declaration without assignment
let t:bool;
t = true;
types
basic types
bool = boolean
str = string literal
string = string (dynamic)
integers
i8 = 8-byte signed integer
i16 = 16-byte signed integer
i32 = 32-byte signed integer
i64 = 64-byte signed integer
i128 = 128-byte signed integer
u8 = 8-byte unsigned integer [0-255]
u16 = 16-byte unsigned integer
u32 = 32-byte unsigned integer
u64 = 64-byte unsigned integer
u128 = 128-byte unsigned integer
boolean logic
a == b
a != b
a || b
a && b
strings
String from str
String::from("...")
trim a string
[string].trim()
change case
[string].to_lowercase()
[string].to_uppercase()
tuples
enum
#[derive(Debug)]
enum [name] {
[value],
...
}
let [name] = [enum]::[value];
structs
define a struct
#[derive(Debug)]
struct [name] {
// fields
[attribute]: [type],
...
}
define custom implementation
impl [name] {
// constructor
fn new([param]...) -> Self {
Self {
[attribute]: [value],
...
}
}
// method
fn [name](&self, [param]...) {
...
}
}
create instance of struct
[struct] { [attribute]:[value], ... }
[struct]::new( [attribute]:[value], ... )
reference to type
Self
reference to instance
self
example: basic struct
#[derive(Debug)]
struct MyStruct {
id: u8,
name: String,
}
println!("{:#?}", MyStruct { id:3, name:String::from("foo") });
example: struct with custom implementation
#[derive(Debug)]
struct MyStruct {
id: u8,
name: String,
}
impl MyStruct {
fn new() -> Self {
Self {
id : 1,
name : String::from("bar")
}
}
fn my_method(&self) {
print!("baz!");
}
}
let v=MyStruct::new();
println!("{:#?}", v);
v.my_method();
arrays
define an array
let list: [i32; 4];
list = [8, 6, 4, 2];
define 2-dimensional array
let mut nested = [[0u8; 75]; 75];
get length
[array].len()
for each (iterate values)
for v in &[array] {
println!("iterate:{}", v);
}
iterate with index
for i in 0 .. myarr.len() {
println!("iterate:{} = {}", i, myarr[i]);
}
vector
define empty vector
let mut [name] = Vec::new();
define vector with values
let mut [name] = vec![...]
add element
[vector].push([value]);
control flow
if else
if [condition] {
...
} else {
...
}
pattern matching
match [value] {
[pattern] => ,
...
}
loop without condition
loop { ... }
for loop with range
for i in 1..10 {
println!("{} {}", "Hello ", i);
}
break
loop {
break;
}
functions
calling a constuctor
[type]::new();
user defined function
fn [name]() { ... }
function with return type
fn [name]() -> [type] { ... }
input / output
print a line to system output
println!("...");
print a variable (plain)
println!("my variable: {}", [variable]);
print a variable (debug output)
println!("my variable: {:?}", [variable]);
print a variable (pretty formatting)
println!("my variable: {:#?}", [variable]);
read a line from the system input
use std::io::stdin;
let mut input = String::new();
stdin().read_line(&mut input).expect("error reading line");
references
https://doc.rust-lang.org/std/macro.println.html
https://doc.rust-lang.org/std/io/struct.Stdin.html
documentation
https://doc.rust-lang.org/std/index.html