DEV Community

Vijesh Salian
Vijesh Salian

Posted on

Starting with Rust

Programming has been my profession for over 10 years now, but I am proficient in only one language. Even though I may use other languages, I cannot call myself proficient in any other than my primary language. That does not make me very comfortable. Learning a different language gives you a much needed different perspective about programming. So I decided to learn other languages. There is a plethora of languages available. I decided to pick 2 languages that bring a slight counter-intuitiveness to my conditioned object-oriented-thinking mind. I picked F# and Rust. I am no language design enthusiast, so I won't be talking about what are the languages' good and bad parts. As a programmer I would like to know the language well enough to be able to build reliable systems. An expertise in the language is required to avoid known pitfalls. In this post, and in a following few, I will be writing about Rust. This post will cover introduction and installation of Rust.

Rust began its journey from within Mozilla. https://research.mozilla.org/rust/. It is an open sourced programming language and is still sponsored by Mozilla. https://github.com/rust-lang/rust.

Rust is known to be a systems programming language. It is also a general purpose programming language from what I see. So what can you build with Rust? Well, you can build web applications, distributed applications, embedded applications and many more. You can even build your own operating system if you want to. What is so special about Rust? Rust was built from scratch as a systems programming language as an alternative to C and C++, but without the known pitfalls of those languages, especially around memory management. It is a modern language that addresses so many of the issues which makes building systems highly resilient which was really hard to achieve with other programming languages. It is said that if you write code that compiles in Rust, you would have written an efficient code without having to do micro-optimizations.

How do you I get started with Rust
Install Rust on your machine from here https://www.rust-lang.org/tools/install.

If you are on Windows make sure to add Rust path in your PATH system variable.

rustup is a command line tool that manages Rust versions and other associated tools required for development.

Update your Rust installation by running the following in your shell.

rustup update

Run this to check the Rust version. If it shows a version then it has installed correctly.

rustc --version

Note the -- before the word version. That is important; its an argument for the command. The first time I tried the command without -- and got this weird error.

error: couldn't read version: The system cannot find the file specified. (os error 2)

error: aborting due to previous error

So, if you see that error, then check the syntax once again, you may be missing --. Also note that I am on Windows 10. Depending on your operating system you may experience it differently.

Okay, now show me the code!

Let's do an obligatory Hello World program.

Create a directory where you would like this program to reside.

mkdir HelloRust
cd HelloRust

Create a file called main.rs. Type the following code in and save the file.

fn main() {
    println!("Hello Rust");
}

Go to the shell and run the command to compile that rust code.

rustc main.rs

And then run the main executable. On Windows it's the main.exe file. You should see Hello Rust.

If you look at the code you will see that it defines a main function and prints a string. If you have been coding before, this syntax is obvious. There may be some things that may not seem familiar like the ! after the println. Well that is something we may need to cover at a later point in time. This post is not meant for that.

If you would like to learn about Rust, the official documentation is fantastic. The official Rust website has a whole well-written book for free. You can find it here. https://doc.rust-lang.org/book/title-page.html

If you would like to know about my experiments with Rust, then follow this space. Thank your for your attention.

Cheers! 🍺

Top comments (0)