...
This command will create an executable in target/release instead of target/debug. The optimizations make your Rust code run faster, but turning them on lengthens the time it takes for your program to compile. This is why there are two different profiles: one for development, when you want to rebuild quickly and often, and another for building the final program you’ll give to a user that won’t be rebuilt repeatedly and that will run as fast as possible. If you’re benchmarking your code’s running time, be sure to run
cargo build --release
and benchmark with the executable in target/release.
Naming Conventions
Rust code uses snake case as the conventional style for function and variable names, in which all letters are lowercase and underscores separate words.
Constants are all uppercase.
Code Block |
---|
const PI: f32 = 3.14;
fn main() {
let my_var = 5;
another_function(my_var);
}
fn another_function(x: i32) {
println!("The value of x is: {x}");
} |
Variables
Immutable/Mutable
...
Code Block |
---|
fn main() { let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; let a: [i32; 5] = [1, 2, 3, 4, 5]; //access values let first = a[0]; let second = a[1]; } |
Functions
Rust code uses snake case as the conventional style for function and variable names, in which all letters are lowercase and underscores separate words
Code Block |
---|
fn main() {
another_function(5);
}
fn another_function(x: i32) {
println!("The value of x is: {x}");
} |
Returning Values
Code Block |
---|
fn main() {
let x = plus_one(5);
println!("The value of x is: {x}");
}
fn plus_one(x: i32) -> i32 {
x + 1
} |
The value returned from function plus_one is the value of x+1 or 6. Notice that there is no semi-colon at the end of the line. This is an expression.
Comments
Rust uses two forward slashes ( // ) to indicate a comment.
For comments that extend beyond a single line, you’ll need to include //
on each line.
Comments can also be placed at the end of lines containing code
Code Block |
---|
// So we’re doing something complicated here, long enough that we need
// multiple lines of comments to do it! Whew! Hopefully, this comment will
// explain what’s going on.
fn main() {
let lucky_number = 7; // I’m feeling lucky today
} |
Control Flow
If Expression
Code Block |
---|
fn main() {
let number = 6;
if number % 4 == 0 {
println!("number is divisible by 4");
} else if number % 3 == 0 {
println!("number is divisible by 3");
} else if number % 2 == 0 {
println!("number is divisible by 2");
} else {
println!("number is not divisible by 4, 3, or 2");
}
} |
Using if in a let Statement
Code Block |
---|
fn main() {
let condition = true;
let number = if condition { 5 } else { 6 };
println!("The value of number is: {number}");
} |
Loops
Code Block |
---|
fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 2;
}
};
println!("The result is {result}");
} |
While Loop
Code Block |
---|
fn main() {
let mut number = 3;
while number != 0 {
println!("{number}!");
number -= 1;
}
println!("LIFTOFF!!!");
} |
IntelliJ IDE
To use Rust in IntelliJ install the following plugins:
...