First Rust experience

Rust is a multi-paradigm general purpose programming language, sponsored originally by Mozilla, and now by the Rust Foundation. Its focus is the memory safety.

Progressively, more technological industries are replacing C/C++ with Rust. It’s by the seventh year in a row, the most loved language according to Stack Overflow 2022 survey, so it’s a great opportunity to me to learn why 😃

Stack Overflow 2022 survey
stackoverflow.com 2022 survey

I am learning about Rust right now, using the Rust book, so I will comment that journey here.

The basic setup needed is a linker (included in GCC/Clang for example) and the Rust toolchain rustup. I am a Linux enthusiast so for me is preferable to develop using it. Right now I run Manjaro so this is what is needed to install it:

$ sudo pacman -S rustup gcc

To do a simple Hello World!, create a Cargo project with cargo new hello_world. This will generate src/main.rs. As occurs on C++, Rust needs a main method as entry point. This is the content of that file:

func main() {
  println!("Hello World!")
}

Similarly to C++, it needs { } to define scope. A particular thing here is that this function starts with func (as occurs with Javascript with function or Python def), and the () are part of the definition.

Also, println! is a macro, not a function. The ! mark is what defines that. Macros and functions are different, but that’s not relevant at the moment.

Finally, to run it, I clicked on the Run button (green one, integrated with the IDE -CLion in this case-) that executes cargo run, it compiles the file, creating a target folder. For common development, it builds a debug folder inside, where the binary is.

Output sample of Rust Hello World!
Output sample

At the moment, easy peasy! Follow the same steps as compiling a C/C++ file using GCC or Clang, the syntax is pretty explicit (something I am grateful of strong-typed languages), use conventional scope and function definitions. It has similarities with C++, but this is just the beginning 😅


Posted

in

by

Tags: