> For educators. It might be tempting to tutor students on C++ a fast and performant language and something that teaches your students about memory management. In general, it's good to consider memory and speed when coding. In the last couple of years, Go as well as Rust has become popular in the space of system programming but also used for the Web, especially with WASM (web assembly). More and more jobs exist on Rust and major companies like us are starting to use it. It's not a beginner language though and have a specific way of dealing with memory via ownership, but it's worth considering.
If you are considering Rust, you most likely have a few different applications in mind that requires speed and effective usage of memory like:
The sales pitch that really sells Rust though is:
Those all sounds good, but let's dive into it and learn to code in it and see what it has to offer.
There're a few different ways to install Rust. The recommended way is to use rustup.
If you feel like evaluating the language first, check out the playground that enables you to write code, compile and run it with no install.
Given that you've installed Rust, you will have access to the compiler rustc
, an executable you use via the command line.
Give it the following content:
fn main() {
println!("Hello world");
}
Compile program with rustc
rustc main.rs
./main
Here's the output:
Hello world
It wasn't much code, but you now have a working application. So, what did you do?
Entrypoint, you defined an entry point to the application a method main()
. This is you telling Rust where to start the program execution. You used the keyword fn
to define a function, followed by the function name "main" and curly braces:
fn main() {}
Printing to the console. You used the print macro, println!
and give it a string literal "Hello world".
fn main() { println!("Hello world"); }
That's it, that's all you needed for a program in Rust. Next, let's look at using variables.
You use variables in Rust to store values that you want to refer later to in code. There are different variable types you can work with, but for now, let's learn how to create a variable and use our println!
macro.
You create a variable by typing:
let name = "Chris";
The above creates a variable name
that you can refer later to in code.
You can now print name
with the println!()
macro like so:
println!("Hi {}", name);
The curly braces {}
interpolates your variable name
and you end up with "Hi Chris" where you to compile and run the code.
Let's actually do that next.
Now that you learned about defining a variable and printing it, lets modify your existing code.
Change app.rs to this code:
fn main() {
let name = "Chris";
println!("Hi {}", name);
}
Compile the program with rustc
:
rustc main.rs
./main # it's an exe file on windows
You now see "Hello Chris"
Congratulations, you've now started your journey to become a programmer in Rust, or as it's also called, a Rustacean.
You learned about Rust, why and where to use it. Additionally, you've created a program in it and you're now ready to learn more about Rust. Welcome Rustacean :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.