Getting started with Rust
Just follow the instructions on rust-lang.com and install the latest version of rust.
I am going to use the new RustRover by JetBrains for this blog but you can use any IDE you like.
Understanding the dir tree
If you are using cargo you can create a new project using
1 | cargo new recho --bin |
the bin flag tells cargo that the program is a binary instead of a lib for which you use –lib. We will come back to lib
in later blogs.
Here is the dir tree
1 | . |
echo echo
One of the most important tasks of echo is to print the stdin
to console.It has many other important tasks like previewing a cmd especially something like rm -rf *
It can tell you about some variable like $ROS_SERVER
and much more.
However today I am going to focus on just the simple printing of stdin
to console.
Strings unattached
If you have worked with any programming language you would have guessed that we are going to work with strings today.
How rust handles strings is completely different than something like python, infact if you have not used a language with types (talking about you javascript) you might feel lost…
But we must march ahead. To access args you can use this
1 | fn main() { |
We define main function like in C. Then we use let
(happy javascript noises) to define variable args. To insert type simply use ‘:’ and you can insert your desired type.
env::args().collect() will allow you to get the input in terms of ‘Vec
Rust does not have classes so “::” allows access to a iterator inside module called args(We will come back to this later).
Based on the ‘trait’ you can perform multiple operations..collect()
here will convert each iterated element to a Vector storing each of them as ‘String’.
Arguments (No one likes arguing)
Args are a good way to get user input in any program
Rust simply takes in the input you attach to your binary for example
1 | cargo run recho Args Kwargs Bargs |
Rust will allow you to access Args Kwargs Bargs
as an element inside a Vector with type Vec<String>
Understanding rust args
The very first element of the args is ususally the path of the executable however it can be gibberish.To conclude conside the first element as rubbish and ignore it.The other elements are as per the input Args Kwargs Bargs
.
The next thing to do is to print the input.To do this I have passed the input to another function (for testing)
Actual echo fn
1 | fn echo_input(args: Vec<String>) -> String { |
-> will allow you to define a type for the ‘return’ value.In this case it is a String.let mut output=String::new()
allows you to define a mutable variable as new empty String.for i in 1.. args.len()
will iterate from 1 to args.len().let input:&String=&args[i]
allows you to define another variable however you will see a new symbol here ‘&’(Happy C and C++ noises).This is called referencing. Rust works on the concept of borrowing once you tell rust that a=b you can not use b later because b now belongs to a.By using & you tell rust that to access the value it can just go to the location of where b is stored in this case args[i].output.push_str(input)
will concatenate the string to the output
string.
Finally I want a whitespace after every element in args so i just concatenate a whitespace except for the last element.
Running the program
To run the program use
1 | cargo run -r Args Kwargs Bargs |
It works!
1 | ➜ recho git:(recho-bringup) ✗ cargo run -r Args Kwargs Bargs |
However if you run
1 | cargo run -r |
Do you get an error?…
In the next blog we will write some tests to confirm that our code works in different cases till then Ciao!