Intro To Rust

Intro To RustRylan BauermeisterBlockedUnblockFollowFollowingMay 15If you’re anything like me, Rust may have snuck up on you.

With so many established programming languages, it can be hard to motivate oneself to learn yet another one, especially one as nascent as Rust.

However, as more and more companies adopt this new language it’s starting to look like it’s here to stay.

This article is aimed at helping you through a couple vital questions:What is Rust?What makes it powerful?What sort of syntax does it use?How does it manage dependencies?Why should I bother learning it?What Is Rust?Originally created by Graydon Hoare for Mozilla, Rust is a static, strongly typed, and compiled language, specializing in quick runtimes and memory management.

In addition to the notable distinction of being the “most loved” language on the Stack Overflow Developer Survey for 2016, 2017, and 2018, Rust is being picked up by an increasing number of major companies.

At present, Rust is in use in some capacity or another by Amazon, Google, Facebook, Microsoft, Dropbox, and Reddit, as well as being utilized by some of the core services of npm.

it is also the 5th fastest growing language on Github, outpacing both Python and Go.

It’s safe to say it’s growing in popularity and strength, which begs the question: why?Fig 1: Rust just wants to make sure you’re okay (image courtesy of David Marino)What Makes It Powerful?Rust wants you to stay safe.

Very safe.

If Rust could tie you up with chains and lock you in the basement, it would.

Some might consider this creepy, but engineers can see through the sinister façade and understand: the computer probably knows best.

Programming in rust can feel very similar to working in other strongly typed languages, but it brings some notables to the table.

First off, every aspect of it is designed to tighten down every loose screw and hammer in every hanging nail.

It automatically declares variables as constants, needing to be told if a variable should, in fact, be mutable.

It makes use of extensive pattern matching to simplify code flow, and can has Ruby-esque automatic returns.

In short, it’s aimed at making code both powerful and understandable, stable and succinct.

It aims to do what other strongly typed languages do, but do it in a way that doesn’t take as long, or require as much from you (aside from surviving its compilation process).

What Sort of Syntax Does It Use?Enough talk.

Let’s jump into some code examples.

Fig 2: Hello, Rust!Here we see a classic Hello World program.

At this point, Rust looks pretty identical to most other languages.

We can parse that fn is telling us that we’re defining a function.

println!.seems to be equivalent to Ruby’s puts, or Java’s System.

out.

println.

Like Java, main is a special method name reserved for the entry point into the program.

Whereas Javascript would require you to do something along the lines of calling main() below the declaration, in Rust the program will run main upon use.

Fig 3: all Rust variables are constant by defaultThe next stumbling block that many will crash into is the fact that in Rust all variables are declared as constants by default.

If you want to be able to reassign values to a variable, you will need to declare them in a special way (see Fig 4).

There is one other thing to note here: the let keyword.

For Javascript users it will be a familiar sight; for Java programmers it might seem like blasphemy.

In truth, Rust utilizes let as a catch-all declaration, then uses type-inference to assign the type to the variable.

Of course, you can expressly declare variable types as well.

The other thing to break down here is that print statement.

Because of how strict it is, Rust requires that a string be passed into the print statement.

However, it will let you pass whatever you want after that, and will replace any {} within the string with those items, in order.

Fig 4: Properly assigning variablesIn this example the opening lines are identical, save for one small difference.

Instead of let a = 1; we’ve declared let mut a = 1.

This means that we are declaring a as a mutable variable, meaning Rust will let us change and reassign it to our heart’s content.

Down below we see an example of type inference, using the String::new() constructor.

In this case, _string is declared as a String via type inference.

However, next we see let a: u32;.

This is an example of assigning a variable to a specific type at declaration.

Given the many different types of numbers available in Rust ( u32 incidentally is “Unsigned 32 Bit"), specifying the precision of number that you wish to use can be a crucial skill to writing efficient code.

We also see that in spite of a being declared initially as immutable, it is assigned on the very next line.

This is because declaration is not assignment.

Immutable variables can only be assigned once, but where they get that assignment doesn’t matter.

Fig 5: MatchThe next Rust oddity you should familiarize yourself with is the match keyword.

This operator works like the switch-case statement’s grown up brother.

You essentially feed it a variable, and it returns a value based on what matches the result.

What’s more, it does it extremely cleanly — match statements are eminently legible.

So, let’s review what Rust requires of you:Constants are declared via either type inference with let, or by direct assignment with a :.

Mutable variables are declared in the same way, but with let mut.

Constants can be declared, then assigned later.

Lines must end with ;.

main is the default function which is run on upon code execution.

match statements can be used to match the value of a variable or statement to a series of potential outcomes (called arms, which then in turn execute code.

This is a pretty rudimentary view of things, but this is enough to get you started out.

For more information, please see The Rust Programming Language, a full length, wonderfully written technical manuscript on Rust and how to use it.

How Does It Manage Dependencies?With the growth of technologies such as Node Packages, Ruby Gems, and the myriad of frameworks scattered about the internet, having a way to manage dependencies and easily build projects is crucial to development in the modern age.

Thankfully, Rust comes with cargo, a built in dependency manager that can help you with all those issues.

Cargo utilizes crates, which are its version of packages.

These can be easily imported, then included via the extern crate command.

For example:Fig 6: importing a crate and using itThis simple example shows us bringing in the rand crate, and using it to generate a random number between 1 and 10.

Easy enough, right?.To add the rand crate, you need to be in a cargo-managed project.

You can make one with the simple command:cargo new [project_name] –binThis makes a new project with all the necessary folders and files for creating dependencies.

Specifically, it will make a file that will look something like this:Fig 7: Cargo.

tomlCargo.

toml is the file managing your dependencies, and is created in the cargo new command.

You can add any and all dependencies your project has to the [dependencies] section, along with their version number.

Then, when you compile your project they (and all their dependencies) will be brought in, compiled, and included.

Simple as that.

Why Should I Bother Learning It?Well, that’s a tough question.

If you already know one of the staple languages, you might not want to pick up a similar one.

Here’s my argument in favor of learning Rust, however:Technology is, and will always be, a shifting landscape.

The more we cling to what we know and refuse to delve into the future, the more we will fail to grow.

Rust is an effort to improve, simplify, and streamline existing technologies.

It handles memory and multithreaded programs like a champ.

It’s growing at a rapid rate.

At present, it looks like a good ship to get on.

If it helps, you can think of learning programming languages like investing in startups.

Sure, sometimes the company may fizzle, but sometimes you’ll strike gold.

Being an early contributor on a language that rises to prominence is no small mark of honor, and it will open doors to careers and opportunities that you otherwise would have missed out on.

In short: it’s growing.

With its current trajectory, Rust will continue to boom.

Learning it now might help.

That aside, it’s a fun language with an enthusiastic, happy community.

And hey, you can write stuff like this:Fig 8: calculate a fibonacci sequence in RustWhich this author at least thinks is pretty dang cool.

Sources & Extra ReadingA Snapshot of Rust's Popularity in July 2018Talking about a language's popularity is traditionally a tricky topic.

How do you measure popularity?.How do you…www.

jonathanturner.

orgProjectsThe State of the Octoverse reflects on 2018 so far, teamwork across time zones, and 1.

1 billion contributions.

octoverse.

github.

comThe Rust Programming Language – The Rust Programming LanguageWelcome to The Rust Programming Language book!.This version of the text assumes you are using Rust 1.

31.

0 or later…doc.

rust-lang.

orgStack Overflow Developer Survey 2018Each year, we ask the developer community about everything from their favorite technologies to their job preferences…insights.

stackoverflow.

comPrimitive Data Types – Getting Started With RustExplore this playground and try new concepts right into your browserwww.

codingame.

com.

. More details

Leave a Reply