0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

#06 What is Rust lang?

Posted at

Introduction

Do you know which programming language has been rated the most "loved" among developers for beyond five years? It's Rust!! The Rust programming language first debuted in 2010. Even though it is a fairly young language compared to C++, and C lang, the language has gained much attention from many developers. Rust is a systems programming language that is designed to be safe, concurrent, and fast. It is also one of the famous open-source programming languages that are maintained by hundreds of contributors, most of them are volunteers. But why do a lot of developers love and choose Rust? Because there are some reasons.

Rust is

  1. High Performance
  2. Reliable
  3. High Productivity

Rust is used by many companies such as

Rust will be supported in the Linux kernel.

Because of such reasons, Rust has been nominated as one of the "love" languages among developers all over the world.

So you want to know more about Rust language? Let's dig into the Rust world.

Background

Firstly, I will explain a little bit of the history of Rust programming language. Rust began as a side project of Graydon Hoare who is a Mozilla Research employee. Mozilla is an open-source browser company. One day, he went back to his apartment, the elevator was out of order; its software had crashed. He got annoyed while he climbed the stairs. He knew the reason why the software crashed. This was because of memory management. The software inside devices like elevators is often written in languages like C++ or C, which are famous for allowing programmers to write code that runs very fast. However, those language cause memory bugs quite often. According to Microsoft, 70% of the vulnerabilities in its code are due to memory errors from code written in these languages. Then he decided to develop a new computer language that is focused on memory safety and is even able to write small, fast code.

Rust's unique features

Rust has some very unique features compared to other languages. For example,

Does not have garbage collection

Current popular programming languages like Java, JavaScript, and Python use garbage collectors. What are garbage collectors? It automatically manages the computer memory to relieve stress on coders. It would periodically clean up the memory as a piece of software was running. But this is not good because garbage collection takes up the crucial processing time. It means these languages used much more memory. Then, Rust took a different way. It would not require programmers to manually figure out where in memory they were putting data. Rust does that.

Ownership

It enables Rust to make memory safety guarantees without needing a garbage collector. Ownership is a set of rules that govern how a Rust program manages memory. Memory is managed through a system of ownership with a set of rules that the compiler checks. If any of the rules are violated, the program won't compile.

This is one of example why Rust will not compile because of the ownership problem.

let s1 = String::from("hello");
let s2 = s1;

println!("{}, world!", s1);
// This is because variable s1 has already moved to variable s2
// so can not use it anymore after moving.

Borrowing

If you want to use a value in a function, without transferring ownership to the function, you can borrow the value temporarily from its owner. When the function is done with the value, it's given back to the owner.

Let's see one example!

fn main() {
    let a = String::from("Hello");
    // reference a with &
    let str_len = get_str_length(&a);
    println!("String length: {}", str_len);
}

fn get_str_length(s:&String) -> usize {
    s.len()
}

Option and Result

Rust has unique enum types Option and Result.

Option type can either be something or nothing.

enum Option<T> {
    None,
    // T is the generic type
    Some(T),
}

Options are commonly paired with a pattern matching to query the presence of a value and take action, always accounting for the None case.

Similar to Option above, there is Result type. This is used for returning and propagating errors. It can be one of two things: it's either an Ok or Error. Ok represent success and contains a value, and Err(E) represents error and contains an error value.

enum Result<T, E> {
    Ok(T),
    Err(E),
}

That's all for a brief introduction. If you don't understand some code or any features, don't worry. It's just started. Next, let's install Rust.

Welcome to Rust

Before writing code in Rust, we need to set up development environment. If you want to try writing code without installing, there is an editor in browser, here.

I would recommend wsl for windows user when you set up new development environment. Otherwise, you can come here to download.

  1. Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# info: installing component 'rustfmt'
# info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'

#  stable-x86_64-unknown-linux-gnu installed - rustc 1.68.0 (2c8cc3432 2023-03-06)


# Rust is installed now. Great!

# To get started you may need to restart your current shell.
# This would reload your PATH environment variable to include
# Cargo's bin directory ($HOME/.cargo/bin).

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

  1. To configure your current shell, run this command or close terminal then open again!
source $HOME/.cargo/env
  1. Check that you installed Rust correctly
cargo --version

# cargo 1.68.0 (115f34552 2023-02-26)
# cargo build system and package manager is similar to npm in JavaScript.

Congulatulations! You finally ready to go rust development.

Hello world

Let's build first small app which is called Hello World app. This will print Hello, world! text.

cargo new hello-world

# Created binary (application) `hello-world` package
cd hello-world

Open your favorite editor. In my case, I mostly use Visual Studio Code. If you use VS Code, I recommend to install this extension.

fn main() {
    println!("Hello, world!");
}

Run app

cargo run

# Running `target/debug/hello-world`
# Hello, world!

Congulatulations! You did it. :clap:

Additional

If you want to dig in more, I can recommend some youtube channels, and books

Blog

Recommend Channel

Recommend books

Thank you for your time. Enjoy your Rust life!

References

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?