Skip to content

Easy to understand applications with rust just for having fun 😌

License

Notifications You must be signed in to change notification settings

1995parham-learning/rust101

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

56 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Rust 101

Learning Rust with the love of your life
GitHub Workflow Status (with event)

Introduction

Easy to understand applications with Rust just for having fun 😌.

Tools

rustc

The Rust compiler

cargo

The Rust dependency/project manager

Rustup

Rustup manages the rust versions, required programs, etc. It is a great program to simplify your rust programming workflow.

Dependencies

Rust handles dependencies by hand in cargo.toml file. In this file you specify the dependencies by their required version.

# Create a new Cargo package
carg new

# Build and install a Rust binary
cargo install

# Compile the current package
cargo build

# Search packages in crates.io
cargo search

# Build a package's documentation
cargo doc

# Execute unit and integration tests of a package
cargo test

# Automatically fix lint warnings reported by rustc
cargo fix

# Execute benchmarks of a package
cargo bench

For finding about latest release of each cargo you can use Docs.rs.

Expression vs Statement

Statement is instruction that performs some action and do not return a value. Expressions evaluate to a resulting value.

The let y = 6 statement doesn't return a value. This is different from what happens in other languages, such as C and Ruby, where the assignment returns the value of the assignment.

These are expressions:

  • The 6 in the statement let y = 6;
  • Calling a function
  • Calling a macro
  • {}

Print

rust has println! macro which has {} for normal printing, {:?} for debug printing and {:p} for pointers.

Match

let some_u8_value = Some(0u8);
if let Some(3) = some_u8_value {
    println!("three");
}

let some\*u8_value = 0u8;
match some_u8_value {
    1 => println!("one"),
      2 => println!("two"),

      - => (),
}

# [derive(Debug)]

enum UsState {
    Alabama,
    // --snip--
}

enum Coin {
    Penny,
    Nickel,
    Dime,
    Quarter(UsState)
}

match coin {
    Coin::Penny => 1,
    Coin::Nickel => 2,
    Coin::Dime => 10,
    Coin::Quarter(state) => {
        println!("State quarter from {:?}!", state);
        25
    },
}

Loops

let result = loop {
    counter += 1;
    if counter == 10 {
        break counter * 2;
    }
};
let a = [10, 20, 30, 40, 50];

for element in a.iter() {
    println!("the value is: {}", element)
}

for number in (1..4).rev() {
    println!("{}!", number)
}

Stack vs. Heap

All data stored on the stack must have a known, fixed size. Data with an unknown size at compile time or a size might change must be stored on the heap instead. string literals (str type) are stored on executable and we know them at the compile time. Strings are stored on heap and we get their memory at the runtime from the heap. We need to free heap memory and rust will do this by calling the drop function on that complex type.

Ownership

Assignments in rust may copy or move the variable if variable has the copy trait rust will uses copy otherwise it uses move. types that doesn't have drop method in them or their parts can have copy trait otherwise there would be a compile error unless you implement the clone trait. With references you can prevent moving variables.

Iterators

iterators have method for casting them into collections which is named /collect/.

Arrays

Rust arrays have fixed size and can be defined in the following ways:

let a1 = [1, 2, 3]
let a2: [u64; 5] = [1, 2, 3, 4, 5]

Invalid array access doesn't cause compile error in Rust but it cause panic at runtime. Go in this case creates compile error.

Slice

Slice is reference to its underlying array:

let s = String::from("hello")

let slice = &s[..]
let slice = &s[0..]

Traits

traits are similar to interface in go. they can be implemented on structures or their references.

Github Actions

Unofficial GitHub Actions for Rust programming language

To Read

Crates

Projects

Phone Book πŸ“±

Phone book application stores users and their phones as struct. It has an uncomplicated menu, and users can add, get, and remove entities.

Word Count πŸ”’

This application receives a delimiter and some entries (i.e. some text files or array of strings) and after separation based on a delimiter, it will count and calculate the number of each word in the entries.

Data in Depth

This example is based on Rust in Action book and shows how data is stored. One of its example use rust overflow panic and if you build it in release mode then you don't have it.

BPF

To write BPF code in Rust, it's easiest to use cargo-bpf (part of the redbpf suite) which handles setting up the project and can even function as a development loader.

sudo pacman -Syu llvm
cargo install cargo-bpf --features=llvm13
cargo bpf new elbpf
cargo bpf add hello

About

Easy to understand applications with rust just for having fun 😌

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages