In this post we will review how to get familiar with Rust programming language.
Useful resource is "The Book", that is the Rust Programming Language book.
Installation
To install Rust, use the following:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
The latest version we're using in this post is 1.69.0. In case of need, rust version can be updated using:
rustup update
Using Rust requires us to use the cargo tool, which is a build tool and a package manager.
Create a New Project
Let start with a new project:
cargo new hello-world
I am using JetBrains GoLand IDE with Rust plugin installed to edit the code, and run the project, but in general, we can run the project using:
cargo run
To add dependencies, add in the project manifest file, Cargo.toml the required libraries:
[dependencies]
ferris-says = "0.3.1"
and now, we can use the dependency:
use ferris_says::say;
use std::io::{stdout, BufWriter};
fn main() {
let stdout = stdout();
let message = String::from("Hello World!");
let width = message.chars().count();
let mut writer = BufWriter::new(stdout.lock());
say(&message, width, &mut writer).unwrap();
}
The output is:
Dependencies
cargo update
To create documentation for the project and all its dependencies, use:
cargo doc --open
Functions
functions:
fn my_function(){
}
Call to function:
my_function();
Call to macro (notice the '!' at the end):
println!()
Function with return value should not include ';' in the return statement (the last statement):
fn add(i1: i32, i2: i32) -> i32 {
i1 + i2
}
alternatively use explicit return statement:
fn add(i1: i32, i2: i32) -> i32 {
return i1 + i2;
}
Variables
Mutable variables:
let non_mutable_variable = 5;
let mut mutable_variable = 4;
// compile error
non_mutable_variable = 8;
// but this one is ok
mutable_variable = 8;
Sending variables by reference:
io::stdin().read_line(&mut guess)
Variable shadowing (useful for convert between types):
let i=5;
let i=String::from("fff");
Constants must include the type as part of the declaration.
const COUNT: u32 = 33;
Tuples
let my_tuple = (1, 'a', "bb");
let first = my_tuple.0;
let third = my_tuple.2;
println!("my tuple elements are {first} {third}")
let empty_tuple =();
Arrays
let my_array = [0, 1, 2, 3, 4, 5];
let array_of_many_fives = [5; 100];
let mut explicit_type_array: [i32; 5] = [0; 5];
explicit_type_array[0] = 1
Ranges
range inclusive
1..=100
range exclusive
1..100
Reversed range:
(0..10).rev()
Loops
While loop:
let mut i = 0;
while i < 10 {
i += 1;
}
Loop on range:
for i in 0..10 {
println!("{i}");
}
Loops and break:
loop {
if 1 == 1 {
break;
}
}
A loop can also return values:
let the_answer = loop {
if 1 == 1 {
break 42;
}
};
println!("{the_answer}")
Conditional Expressions
let number = 5;
let other_number = if number > 5 { "big" } else { "small" };
println!("{}", other_number);
Results and ARM
Handling Result type:
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("numbers only please");
continue;
}
};
arm: pattern to match against, and code to run:
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => println!("You win!"),
}
IO Library
to use the library:
use std::io;
Print macro with variables:
println!("You guessed: {guess}");
No comments:
Post a Comment