Code Examples in Rust. Reviewing RUST

Overview

There are some RUST example code here.

Run like this

cargo run --example enums
cargo run --example iterator
...

You can learn about RUST coding from here.

Rust programming language

Installation

Linux or MacOS

  • Install
$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
  • Update using $ rustup update

Commands

  • View installed version via $ rustup show
  • Check latest version via $ rustup check

Often, cargo check is much faster than cargo build, because it skips the step of producing an executable. If you’re continually checking your work while writing the code, using cargo check will speed up the process! As such, many Rustaceans run cargo check periodically as they write their program to make sure it compiles. Then they run cargo build when they’re ready to use the executable.

Getting Started

Code

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

Compile

$ rustc hello.rs

Output

$ ./hello

Concepts

  • .. used for range like 1..4 i.e. 1, 2, 3. But, if 1..=4 i.e. 1, 2, 3, 4
  • There are different types of struct
    • normal struct: with parameters
    • unit struct: without parameters

“Ownership is Rust’s most unique feature, and it enables Rust to make memory safety guarantees without needing a garbage collector.”

Borrow Checker: You can move the data itself and give up ownership in the process, create a copy of the data and pass that along, or pass a reference to the data and retain ownership, letting the recipient borrow it for a while. The most appropriate approach depends entirely on the situation. Try this

  • Stack (fixed size like char, bool, int; less costly; quick to access by calling var like easy to copy the var) | Heap (variable size like string, list, class; more costly; access var or object via pointer)
  • By default, all the variables are defined as immutable equivalent to const in JS/TS.
  • In Rust, borrowing is analogous to referencing in C++ & dereferencing is same as that of C++.
  • The value of mutable variable can be changed, but not the type.
  • In Rust, every value has a single owner that determines its lifetime.
  • The memory of the declared variables are dropped (or freed) when the program leaves a block in which the variable is declared.
    • E.g. Normally, inside the main function, whenever a variable is defined, it is dropped after exiting the main function.
fn main() {
    // Case-1
    let x = 10;
    let r = &x;

    let k;
    {
        let y = Box::new(5);            // Using Box pointer for storing into heap
        let y = 5;              // stored in stack
        // let y <'a> = 5;
        // k = &y;         // y dropped here as it is not available for lifetime. Moreover the block is getting over after this
        k = y;          // this implies that the ownership of 5 is transferred to `k` from `y`
    }
}
  • Rust doesn't allow dangling pointer by design. This means that any variable, struct, enum, etc can't live more than the lifetime of the referenced type
struct Config {

}

// INCORRECT ❌
struct App {
    config: &Config     // `Config` used as reference
}

// CORRECT ✅
/// Here, it is used as lifetime ownership of the code.
struct App<'a> {
    config: &'a Config
}
  • lifetimes are a compile-time feature and don’t exist at runtime.
  • Rust memory safety is based on this rule: Given an object T, it is only possible to have one of the following:
    • Having several immutable references (&T) to the object (also known as aliasing).
    • Having one mutable reference (&mut T) to the object (also known as mutability).
  • Apply #[derive(Debug)] for making the struct, enum printable
  • Apply #[derive(Clone)] for making the struct, enum copyable.
  • Option vs Result
Option Result
Some or None Ok or Err
An optional value can have either Some value or no value/ None. A result can represent either success/ Ok or failure/ Err
The Option type is a way to use Rust’s type system to express the possibility of absence Result expresses the possibility of error
mainly used for var, function output. For struct, the parameters can have Option type. E.g. In full name, middle_name can be missing for cases, so define middle_name: Option<String> mainly used for operation, function. As normally a variable won't have Err unless there is some calculation involved with this var
Don't want to print the exact issue as None doesn't have anything as param unlike Some(T) Want to print the exact issue as Err(E) contains the message inside
E.g. "./tuts/error_handling/opt" E.g. "./tuts/error_handling/res"

Basics

Primitive types and Variables

  1. Various sizes of integers, signed and unsigned (i32, u8, etc.)
  2. Floating point types f32 and f64.
  3. Booleans (bool)
  4. Characters (char). Note these can represent unicode scalar values (i.e. beyond ASCII)

Print

    1. formatting variables inside println function
let name = "Abhijit";
let age = 28;

println!("My name is {name}, and age is {age}");					// ❌
println!("My name is {0}, and age is {1}", name, age);		// ✔️
println!("My name is {}, and age is {}", name, age);			// ✔️
    1. Multiple usage of variables without repetition
let alice = "Alice";
let bob = "Bob";

println!("{0}, this is {1}. {1}, this is {0}", alice, bob);

Variables

Tools

Troubleshoot

1. warning: path statement with no effect

  • Cause: there is a statement having no effect
  • Solution: Assign the variable to _.

Before:

    let result = match grade {
        "A" => { println!("Excellent!"); },
        "B" => { println!("Great!"); },
        "C" => { println!("Good"); },
        "D" => { println!("You passed"); },
        "F" => { println!("Sorry, you failed"); },
        _ => { println!("Unknown Grade"); }
    };

    result;

After:

    let result = match grade {
        "A" => { println!("Excellent!"); },
        "B" => { println!("Great!"); },
        "C" => { println!("Good"); },
        "D" => { println!("You passed"); },
        "F" => { println!("Sorry, you failed"); },
        _ => { println!("Unknown Grade"); }
    };

    // result;             // warning: path statement with no effect, Solution --> assign to `_` 
    let _ = result;

2. warning: variant is never constructed, error[E0277]: UsState doesn't implement Debug

  • Cause: It simply means that the variant is never used, "constructed", anywhere in your program. There is no AppAction::Task anywhere in the program. Rust expects that if you say an enum variant exists, you will use it for something somewhere.
  • Solution: by putting this before the enum, or individually before intentionally unused items, you can make the warning disappear:

Before:

enum UsState {
	California,
	Mexico,
	Alaska,
}

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

After:

#[allow(dead_code)]
#[derive(Debug)]		// this use is recommended, otherwise there is error.
enum UsState {
	California,
	Mexico,
	Alaska,
}

#[allow(dead_code)]
enum Coin {
	Penny,
	Nickel,
	Dime,
	Quarter,
	Custom(UsState),
}

3. Error: "move occurs...which does not implement the Copy trait"

  • Cause: Copy designates types for which making a bitwise copy creates a valid instance without invalidating the original instance.

This isn't true for String, because String contains a pointer to the string data on the heap and assumes it has unique ownership of that data. When you drop a String, it deallocates the data on the heap. If you had made a bitwise copy of a String, then both instances would try to deallocate the same memory block, which is undefined behaviour.

  • Solution: Just use format like this:

Before:

impl Detail for Car {
    fn brand(&self) -> String {
        return self.brand;           
    }
    fn color(&self) -> String {
        return self.color;
    }    
}

After:

impl Detail for Car {
    fn brand(&self) -> String {
        // using `format` instead of directly returning the brand bcoz it throws error:
        // "move occurs because `self.brand` has type `String`, which does not implement the `Copy` trait"
        return format!("{}", self.brand);           
    }
    fn color(&self) -> String {
        return format!("{}", self.color);
    }    
}

References

Blogs

You might also like...
Some UwU and OwO for your Rust code

UwU Types Some UwU and OwO for your Rust code This is a Rust crate inspired by this tweet from @thingskatedid / @katef. Credits Some extra functionali

The source code that accompanies Hands-on Rust: Effective Learning through 2D Game Development and Play by Herbert Wolverson
The source code that accompanies Hands-on Rust: Effective Learning through 2D Game Development and Play by Herbert Wolverson

Hands-on Rust Source Code This repository contains the source code for the examples found in Hands-on Rust. These are also available from my publisher

This repository contains the Rust source code for the algorithms in the textbook Algorithms, 4th Edition

Overview This repository contains the Rust source code for the algorithms in the textbook Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

Source code for the book Rust in Action

Welcome to Rust in Action source code This source code repository is a companion to the Rust in Action book available from Manning Publications. Suppo

Code to follow along the "Zero To Production" book on API development in Rust.

Zero To Production / Code (Chapter 10 - Part 1) Zero To Production In Rust is an opinionated introduction to backend development using Rust. This repo

Rust crate to implement a counterpart to the PBRT book's (3rd edition) C++ code.
Rust crate to implement a counterpart to the PBRT book's (3rd edition) C++ code.

Rust crate to implement a counterpart to the PBRT book's (3rd edition) C++ code.

A curated list of Rust code and resources.

Awesome Rust A curated list of Rust code and resources. If you want to contribute, please read this. Table of contents Applications Audio and Music Cr

This project contains small exercises to get you used to reading and writing Rust code
This project contains small exercises to get you used to reading and writing Rust code

rustlings 🦀 ❤️ Greetings and welcome to rustlings. This project contains small exercises to get you used to reading and writing Rust code. This inclu

Rust-only ext4 implementation without unsafe code.

Rust-Ext4 Rust-only ext4 implementation without unsafe code. Supporting features no_std Direct/Indirect Block Addressing (RO) Extent Tree Addressing (

Owner
James Johnson
@Tracsell former CTO | @RubyOnWorld Organizer
James Johnson
A collection (eventually) of examples that use some non-beginner things.

nannou examples A collection (eventually) of examples that use some non-beginner things. Right now the only example combines nannou's standard draw AP

Alexis Andre 22 Oct 21, 2022
An inquiry into nondogmatic software development. An experiment showing double performance of the code running on JVM comparing to equivalent native C code.

java-2-times-faster-than-c An experiment showing double performance of the code running on JVM comparing to equivalent native C code ⚠️ The title of t

xemantic 49 Aug 14, 2022
Leetcode Solutions in Rust, Advent of Code Solutions in Rust and more

RUST GYM Rust Solutions Leetcode Solutions in Rust AdventOfCode Solutions in Rust This project demostrates how to create Data Structures and to implem

Larry Fantasy 635 Jan 3, 2023
Rust Sandbox [code for 15 concepts of Rust language]

Rust-Programming-Tutorial Rust Sandbox [code for 15 concepts of Rust language]. The first time I've been introduced to Rust was on January 2022, you m

Bek Brace 4 Aug 30, 2022
TypeRust - simple Rust playground where you can build or run your Rust code and share it with others

Rust playground Welcome to TypeRust! This is a simple Rust playground where you can build or run your Rust code and share it with others. There are a

Kirill Vasiltsov 28 Dec 12, 2022
In this repository you can find modules with code and comments that explain rust syntax and all about Rust lang.

Learn Rust What is this? In this repository you can find modules with code and comments that explain rust syntax and all about Rust lang. This is usef

Domagoj Ratko 5 Nov 5, 2022
:crab: Small exercises to get you used to reading and writing Rust code!

rustlings ?? ❤️ Greetings and welcome to rustlings. This project contains small exercises to get you used to reading and writing Rust code. This inclu

The Rust Programming Language 33.1k Jan 2, 2023
Crabzilla provides a simple interface for running JavaScript modules alongside Rust code.

Crabzilla Crabzilla provides a simple interface for running JavaScript modules alongside Rust code. Example use crabzilla::*; use std::io::stdin; #[i

Andy 14 Feb 19, 2022
Shuttle is a library for testing concurrent Rust code

Shuttle Shuttle is a library for testing concurrent Rust code. It is an implementation of a number of randomized concurrency testing techniques, inclu

Amazon Web Services - Labs 373 Dec 27, 2022
a cheat-sheet for mathematical notation in Rust 🦀 code form

math-as-rust ?? Based on math-as-code This is a reference to ease developers into mathematical notation by showing comparisons with Rust code.

Eduardo Pereira 13 Jan 4, 2023