Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to Rust?

Now, that is an interesting question about an interesting programming language.

We will explore the key concepts of this language through a series of examples. Once we have covered the basics, we will implement some common algorithms using Rust.

If you are new to Rust, you can follow the trainings and books mentioned in the reference section.

Getting Started

Let's step into the world of Rust.

Installation

# On Mac, use homebrew
brew install rustup

# On Linux
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Once rustup is installed, use this command
rustup-init

rustup-init, which will install Rust and the Rust Toolchain, which includes the following:

  • rustc - the Rust compiler
  • cargo - the Rust package manager
  • rustup - the Rust toolchain manager

Validate Setup

First, let us validate the setup. We should have the rustc and cargo commands available.

$ rustc --version
rustc 1.83.0 (90b35a623 2024-11-26)

$ cargo --version
cargo 1.83.0

Rust has a six-week, rapid release process. To update the rust installation, use the rustup update utility.

IDE Setup

If you are comfortable wih a vim like experience consider Helix Editor

# On a Mac
$ brew install helix rust-analyzer llvm marksman

# On Ubuntu 
## Add ppa
$ sudo add-apt-repository ppa:maveonair/helix-editor
$ sudo apt update

$ sudo apt install helix xclip rust-analyzer llvm

VSCode

You can follow this guide


Hello World

Create a new file named main.rs and use your editor to write the following code into it:

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

Use rustc to compile your program into an executable file.

$ rustc main.rs
./main

Hello Cargo

What is Cargo? Cargo is Rust’s build system and package manager. Most Rustaceans use it to manage their Rust projects because Cargo handles a lot of tasks for you, such as building your code, downloading the libraries your code depends on, and building those libraries.

Now, let's use Cargo to generates a boilerplate project for us. The command is cargo new <program_name>.

# 
$ cargo new hello-cargo

$ cd hello-cargo

# Now run the program
$ cargo run

It consists of a new directory named hello-cargo with a src subdirectory and adds two files: Cargo.toml and main.rs.


List of exercises

We have lots of things to cover so here is a step-wise exploration of the key topics.


Topics:


Code:

Sequence Topic Description
1 HelloWorld! Getting started with Rust
2 Variables and DataTypes Exploring datatypes
3 Structs and Traits Exploring Structs and Traits
4 Control flow Exploring control flows
5 Functions and Closures Exploring functions and closures
6 Collections Exploring sequences, maps and sets
7 Custom libraries How to create and use libraries
8 Strings Exploring Strings, Ownerships, References and multable references
8 Stack and Heap Exploring Stack and Heap
10 References and Slices Exploring Rust References and slices
11 Generics Exploring Generics in Rust
12 File I/O Exploring File I/O in Rust
13 Threads Exploring Threads in Rust
13 Mutexes and Arc Exploring Mutexes in Rust

You can find answers to common questions here

Next we implemented some algorithms

Sequence Topic Description
1 LinkedList using Vector Linked List implementation using Vec

References


About

How to Rust?

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages