What is Rust ? How to start ?
Rust ? what is Rust? How to start?
As you might already know Rust is a programming language targeted for low-level development , systems programming, web assembly, and also all related to networking. Nevertheless, there are frameworks and libraries that let you do web development with Rust.
So lets start with an example to understand Rust , which usually helps programmers to udnerstand code by practice.
Setup Rust in your computer
First install Rust by the following official website.
Then use cargo to create a new project "cargo new new-project"
Finally, you will see that a new folder with the name of your project has been created
Start programming
The goal for this example would be to obtain random names and save them into a json file.
As the default main.ts file is shown inside your project folder src , you will see a main function, which works as in C/C++ so your app will be executed in this function.
For this example I will create a console app so I will need to have the args, how could we do that ? answer: with the env::args() function, but first you need to "use std::env;"
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
}
Now what should we do with those ? lets say that the user would like to introduce his name , a configuration file path and a result path for the result in a separate file. To do this I will introduce you to the functions of Rust , to declare a function you need the "fn" key word next to the name of your function, following the "normal" way of declare arguments for a function but the way the function know what type to return is by the "->" operator. For this case I would like to get the user name, the file path and the result path in just one function, which means we need to return a tuple. In simple terms a tuple is a collection of variables, not to be confused with arrays or vectors.
So once the args vector of strings comes to the function it will check one by one if the lenght of the vector is consistent with the arg position. How ? with an if and else statement. Unfortunately for C++ and JavaScript fans we do not have smaller versions of if/else like ternary operators, well not yet.... I know I would like to have the && , ?? and the ?: everywhere.
Unlike other languages we do not require a return to end a function we could simply skip the semicolom(;) and the function will return the value, which in this case is a tuple.
fn get_parsed_args(args: Vec<String>) -> (String, String, String) {
let user_name = if args.len() > 1 { &args[1] } else { defaults::DEFAULT_USER_NAME };
let file_path = if args.len() > 2 { &args[2] } else { defaults::DEFAULT_FILE_PATH };
let result_path = if args.len() > 3 { &args[3] } else { defaults::DEFAULT_RESULT_PATH };
(user_name.to_string(), file_path.to_string(), result_path.to_string())
}
Now several questions might arrice , like what the f he is using & and to_string() ?
So Rust actually comes with a concept of ownership which means "if is in a scope then you do not need this in any other scope"(which computer scientists say is safer than value or reference explicit behaviors). Then in order to use something as reference you will need the &.
Now lets use this function in our main.
fn main() {
let args: Vec<String> = env::args().collect();
let (user_name, file_path, result_path) = get_parsed_args(args);
}
Okay now I see you asking why ? well this is called deconstruction and you might used this in JavaScript, but this time is used in a tuple, so individual parts of it can be used as individual variables.
Random numbers generation with Rust
Lets now say that we want random years , so how to do that ?
First of all we need to set our limits to get the random numbers , lets say that we want the current year , then how do we get the year ? by using a package called chrono , it has functions to know the date and work with it. So I used chrono::Utc:now() to get the current date, then to extract the year I used the .year() function.
Finally, the random numbers , like the date we could use a package for the random numbers called rand, this contains a function rand::thread_rng().gen_range(<your range here>). Inside the range lets put 1991 until the year that we found in the previous parragraph so in Rust code it would be 1991..year
use chrono::Datelike;
use rand::Rng;
fn get_random_year() -> i32 {
let current_date = chrono::Utc::now();
let year = current_date.year();
let rand_year = rand::thread_rng().gen_range(1991..year);
return rand_year;
}
sjkahjkasdhjksahds
Now our main function should look like this
fn main() {
let args: Vec<String> = env::args().collect();
let (user_name, file_path, result_path) = get_parsed_args(args);
let rand_year = get_random_year();
}
Comments
Post a Comment