Getting Started With Rust

Tooba Malaika
5 min readOct 30, 2020
Complete Intro For Rust Language

Rust is a language empowering everyone to build reliable and efficient software.

Introduction

The Rust programming language helps you write faster, more reliable software. High-level ergonomics and low-level control are often at odds in programming language design; Rust challenges that conflict. Through balancing powerful technical capacity and a great developer experience, Rust gives you the option to control low-level details (such as memory usage) without all the hassle traditionally associated with such control.

What is Rust?

Rust is a low-level statically-typed multi-paradigm programming language that’s focused on safety and performance.

Rust solves problems that C/C++ has been struggling with for a long time, such as memory errors and building concurrent programs.

It has three main benefits:

  • better memory safety due to the compiler;
  • easier concurrency due to the data ownership model that prevents data races;
  • zero-cost abstractions.

Let’s go through each of these in turn.

No segfaults

If you want to do system programming, you need the low-level control that memory management provides. Unfortunately, manual management comes with a lot of issues in languages like C. Despite the presence of tools like Valgrind, catching memory management problems is tricky.

Rust prevents these issues. Rust’s ownership system analyses the program’s memory management at compile-time, making sure that bugs due to poor memory management can’t happen and that garbage collection is unnecessary.

Furthermore, if you want to do super-optimized implementations in a C-like manner, you can do that while expressly separating them from the rest of the code with the unsafe keyword.

Easier concurrency

Due to the borrow checker, Rust can prevent data races at compile-time.

Data races occur when two threads access the same memory at the same time, and they can lead to some nasty, unpredictable behavior. Thankfully, preventing undefined behavior is all what Rust is about.

Zero-cost abstractions

Zero-cost abstractions make sure that there is virtually no runtime overhead for the abstractions that you use. In simpler words: there is no speed difference between low-level code and one written with abstractions.

Are these things important? Yes. For example, around 70 % of the issues addressed by Microsoft in the past 12 years have been memory errors. Same with Google Chrome.

What is Rust good for?

Rust being a rather low-level language, it’s useful when you need to squeeze more out of the resources you have. Since it’s statically typed, the type system helps you deter certain classes of bugs during compilation. Therefore, you will tend to use it when your resources are limited, and when it is important that your software doesn’t fail. In contrast, high-level dynamically typed languages like Python and JavaScript are better for things like quick prototypes.

Here are some of Rust’s use cases:

  • Powerful, cross-platform command-line tools.
  • Distributed online services.
  • Embedded devices.
  • Anywhere else you would need systems programming, like browser engines and, perhaps, Linux kernel.

For example, here are a few operating systems, written in Rust: Redox, intermezzOS, QuiltOS, Rux, Tock.

Is Rust object-oriented?

Who knows what object-oriented means nowadays?

The answer is not really. Rust has some object-oriented features: you can create structs, and they can contain both data and associated methods on that data, which is kind of similar to classes minus inheritance. But in contrast to languages like Java, Rust doesn’t have inheritance and uses traits to achieve polymorphism instead.

Is Rust a functional programming language?

Even though Rust is superficially quite similar to C, it is heavily influenced by the ML family of languages. (This family includes languages like OCaml, F#, and Haskell.) For example, Rust traits are basically Haskell’s typeclasses, and Rust has very powerful pattern matching capabilities.

Rust does feature more mutability than functional programmers would usually be accustomed to. We can think of it like this: both Rust and FP try to avoid shared mutable state. While FP is focused on avoiding mutable state, Rust tries to avoid the shared part of the danger. Rust is also missing a lot of stuff that would make functional programming doable in it, such as tail call optimization and good support for functional data structures.

All in all, there is enough support for functional programming in Rust for somebody to have written a book about it.

Is Rust good for game development?

Theoretically, yes. Since Rust is focused on performance and does not use a garbage collector, games written in it should be performant and predictably fast.

Unfortunately, the ecosystem is still young, and there is nothing written in Rust that would compare to Unreal Engine, for example. The pieces are there, though, and Rust has a lively community. If you want to see examples of games written in Rust, you can go to the Rust game dev subreddit.

More on Rust game dev: Are we game yet?

Is Rust good for web development?

Rust has multiple frameworks for web development like Actix Web and Rocket that are very usable and well-built. In particular, if you are looking for pure speed, Actix Web hits the top of framework benchmarks.

Rust doesn’t have anything that can compete with the ecosystem of frameworks like Django and Rails, though. Since Rust is a rather young language, a lot of handy utility libraries are missing, which means that the development process is not that simple and easy

Why Rust ?

  • Rust is blazingly fast and memory-efficient: with no runtime or garbage collector, it can power performance-critical services, run on embedded devices, and easily integrate with other languages.
  • Rust’s rich type system and ownership model guarantee memory-safety and thread-safety. enabling you to eliminate many classes of bugs at compile-time.
  • Rust has great documentation, a friendly compiler with useful error messages, and top-notch tooling. an integrated package manager and build tool, smart multi-editor support with auto-completion and type inspections, an auto-formatter, and more.

Build In Rust ?

In 2018, the Rust community decided to improve programming experience for a few distinct domains. For these, you can find many high-quality crates and some awesome guides on how to get started.

Building Tools

Whip up a CLI tool quickly with Rust’s robust ecosystem. Rust helps you maintain your app with confidence and distribute it with ease.

Web Apps

Use Rust to supercharge your JavaScript, one module at a time. Publish to npm, bundle with webpack, and you’re off to the races.

Servers

Predictable performance. Tiny resource footprint. Rock-solid reliability. Rust is great for network services.

Embedded

Targeting low-resource devices? Need low-level control without giving up high-level conveniences? Rust has you covered.

--

--