aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Programming
  • Tools

Learn Rust In 2022

  • Aelia Vita
  • January 19, 2022
  • 4 minute read

If you’re going to explore Rust this year, download our free Rust cheat sheet, so you have a quick reference for the basics.

Rust is a relatively new programming language, and it’s already a popular one winning over programmers from all industries. Still, it’s also a language that builds on everything that’s come before. Rust wasn’t made in a day, after all, so even though there are concepts in Rust that seem wildly different from what you might have learned from Python, Java, C++, and so on, they all have a foundation in the same CPU and NUMA architecture you’ve always been (whether you know it or not) interacting with, and so some of what’s new in Rust feels somehow familiar.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

Now, I’m not a programmer by trade. I’m impatient yet obsessive. If a language doesn’t help me get the results I want relatively quickly, I rarely find myself inspired to use it when I need to get something done. Rust tries to bring into balance two conflicting things: The modern computer’s need for secure and structured code, and the modern programmer’s desire to do less work while attaining more success.

Install Rust

The rust-lang.org website has great documentation on installing Rust, but usually, it’s as simple as downloading the sh.rustup.rs script and running it.

$ curl –proto ‘=https’ –tlsv1.2 -sSf https://sh.rustup.rs

$ less sh.rustup.sh

$ sh ./sh.rustup.rs

No classes

Rust doesn’t have classes and does not use the class keyword. Rust does have the struct data type, however, its purpose is to serve as a kind of template for a collection of data. So instead of creating a class to represent a virtual object, you can use a struct:

struct Penguin {
genus: String,
species: String,
extinct: bool,
classified: u64,
}

You can use this similar to how a class is used. For instance, once a Penguin struct is defined, you can create instances of it, and interact with that instance:

struct Penguin {
genus: String,
species: String,
extinct: bool,
classified: u64,
}fn main() {
let p = Penguin { genus: “Pygoscelis”.to_owned(),
species: “R adeliæ”.to_owned(),
extinct: false,
classified: 1841 };println!(“Species: {}”, p.species);
println!(“Genus: {}”, p.genus);
println!(“Classified in {}”, p.classified);
if p.extinct == true {
println!(“Sadly this penguin has been made extinct.”);
}

Read More  The Ultimate Guide To Product Bugs: Part 1

}

Using the impl data type in conjunction with the struct data type, you can implement a struct containing functions, and you can add inheritance and other class-like features.

Functions

Functions in Rust are a lot like functions in other languages. Each one represents a discreet set of tasks that you can call upon when needed. The primary function must be called main.

Functions are declared using the fn keyword, followed by the function’s name and any parameters the function accepts.

fn foo() {
let n = 8;
println!(“Eight is written as {}”, n);
}

Passing information from one function to another gets done with parameters. For instance, I’ve already created a Penguin class, and I’ve got an instance of a penguin as p, so passing the attributes of p from one function to another requires me to specify p as an accepted Penguin type for its destination function.

fn main() {
let p = Penguin { genus: “Pygoscelis”.to_owned(),
species: “R adeliæ”.to_owned(),
extinct: false, classified: 1841 };
printer(p);
}fn printer(p: Penguin) {
println!(“Species: {}”, p.species);
println!(“Genus: {}”, p.genus);
println!(“Classified in {}”, p.classified);
if p.extinct == true {
println!(“Sadly this penguin has been made extinct.”);
}
}

Variables 

Rust creates immutable variables by default. That means that a variable you create cannot be changed later. This code, humble though it may be, cannot be compiled:

fn main() {
let n = 6;
let n = 5;
}

However, you can declare a mutable variable with the keyword mut, so this code compiles successfully:

fn main() {
let mut n = 6;
println!(“Value is {}”, n);
n = 5;
println!(“Value is {}”, n);
}

Compiler 

The Rust compiler, at least in terms of its error messages, is one of the nicest compilers available. When you get something wrong in Rust, the compiler makes a sincere effort to tell you what you did wrong. I’ve actually learned many nuances of Rust (insofar as I understand any nuance of Rust) just by learning from compiler error messages. Even when an error message is too obscure to learn from directly, it’s almost always enough for an internet search to explain.

Read More  Edit Video On Linux With This Python App

The easiest way to start a Rust program is to use cargo, the Rust package management and build system.

$ mkdir myproject
$ cd myproject
$ cargo init

This creates the basic infrastructure for a project, most notably a main.rs file in the src subdirectory. Open this file and paste in the example code I’ve generated for this article:

struct Penguin {
genus: String,
species: String,
extinct: bool,
classified: u64,
}fn main() {
let p = Penguin { genus: “Pygoscelis”.to_owned(), species: “R adeliæ”.to_owned(), extinct: false, classified: 1841 };
printer(p);
foo();
}fn printer(p: Penguin) {
println!(“Species: {}”, p.species);
println!(“Genus: {}”, p.genus);
println!(“Classified in {}”, p.classified);
if p.extinct == true {
println!(“Sadly this penguin has been made extinct.”);
}
}

fn foo() {
let mut n = 6;
println!(“Value is {}”, n);
n = 8;
println!(“Eight is written as {}”, n);
}

To compile, use the cargo build command:

<span class="co4">$ </span>cargo build

To run your project, execute the binary in the target subdirectory, or else just use cargo run:

$ cargo run
Species: R adeliæ
Genus: Pygoscelis
Classified in 1841
Value is 6
Eight is written as 8

Crates

Much of the convenience of any language comes from its libraries or modules. In Rust, libraries get distributed and tracked as “crates”. The crates.io website is a good registry of community crates.

To add a crate to your Rust project, list them in the Cargo.toml file. For instance, to install a random number function, I use the rand crate, with * serving as a wildcard to ensure that I get the latest version at compile time:

[package]
name = “myproject”
version = “0.1.0”
authors = [“Seth <[email protected]>”]
edition = “2022”[dependencies]
rand = “*”

Using it in Rust code requires a use statement at the top:

<span class="kw1">use</span> rand<span class="sy0">::</span><span class="me1">Rng</span><span class="sy0">;</span>

Some sample code that creates a random seed and then a random range:

fn foo() {
let mut rng = rand::thread_rng();
let mut n = rng.gen_range(1..99);println!(“Value is {}”, n);
n = rng.gen_range(1..99);
println!(“Value is {}”, n);
}

You can use cargo run to run it, which detects the code change and triggers a new build. The build process downloads the rand crate and all the crates that it, in turn, depends upon, compiles the code, and then runs it:

$ cargo run
Updating crates.io index
Downloaded ppv–lite86 v0.2.16
Downloaded 1 crate (22.2 KB) in 1.40s
Compiling libc v0.2.112
Compiling cfg–if v1.0.0
Compiling ppv–lite86 v0.2.16
Compiling getrandom v0.2.3
Compiling rand_core v0.6.3
Compiling rand_chacha v0.3.1
Compiling rand v0.8.4
Compiling rustpenguin v0.1.0 (/home/sek/Demo/rustpenguin)
Finished dev [unoptimized + debuginfo] target(s) in 13.97s
Running `target/debug/rustpenguin`Species: R adeliæ
Genus: Pygoscelis
Classified in 1841
Value is 70
Value is 35

Rust cheat sheet

Rust is a supremely pleasant language. Thanks to its integration with online registries, its helpful compiler, and its almost intuitive syntax, it feels appropriately modern.

Read More  Go 1.18 Beta 1 Is Available, With Generics

Make no mistake, though, it’s also a complex language, with strict data types, strongly scoped variables, and many built-in methods. Rust is worth looking at, and if you’re going to explore Rust, then you should download our free Rust cheat sheet, so you have a quick reference for the basics. The sooner you get started, the sooner you’ll know Rust. And, of course, you should practice often to avoid getting rusty.

This feature was originally appeared in opensource.com


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

Aelia Vita

Related Topics
  • Open Source
  • Programming Language
  • Rust programming
You May Also Like
zedreviews-Apple-iPhone-16-Pro-finish-lineup-240909
View Post
  • Featured
  • Gears
  • Tech
  • Technology
  • Tools

Apple debuts iPhone 16 Pro and iPhone 16 Pro Max

  • September 10, 2024
zedreviews-Apple-AirPods-Active-Noise-Cancellation-240909
View Post
  • Featured
  • Gears
  • Tech
  • Technology
  • Tools

Apple introduces AirPods 4 and the world’s first all-in-one hearing health experience with AirPods Pro 2

  • September 10, 2024
Automation
View Post
  • Automation
  • Platforms
  • Tools

Automate Your Data Warehouse Migration To BigQuery With New Data Migration Tool

  • August 24, 2023
Developers | Software | Program | Engineering
View Post
  • Software Engineering
  • Technology
  • Tools

Top IDEs And Compilers For C++.

  • July 4, 2023
View Post
  • Architecture
  • Data
  • Engineering
  • People
  • Programming
  • Software Engineering
  • Technology
  • Work & Jobs

Predictions: Top 25 Careers Likely In High Demand In The Future

  • June 6, 2023
View Post
  • Engineering
  • Tools

Red Hat Puts Podman Container Management On The Desktop

  • May 30, 2023
View Post
  • Engineering
  • Practices
  • Tools

Tricentis Launches Quality Engineering Community ShiftSync

  • May 23, 2023
View Post
  • Programming
  • Software Engineering
  • Technology

Build a Python App to Alert You When Asteroids Are Close to Earth

  • May 22, 2023

Stay Connected!
LATEST
  • college-of-cardinals-2025 1
    The Definitive Who’s Who of the 2025 Papal Conclave
    • May 7, 2025
  • conclave-poster-black-smoke 2
    The World Is Revalidating Itself
    • May 6, 2025
  • oracle-ibm 3
    IBM and Oracle Expand Partnership to Advance Agentic AI and Hybrid Cloud
    • May 6, 2025
  • 4
    Conclave: How A New Pope Is Chosen
    • April 25, 2025
  • Getting things done makes her feel amazing 5
    Nurturing Minds in the Digital Revolution
    • April 25, 2025
  • 6
    AI is automating our jobs – but values need to change if we are to be liberated by it
    • April 17, 2025
  • 7
    Canonical Releases Ubuntu 25.04 Plucky Puffin
    • April 17, 2025
  • 8
    United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services
    • April 15, 2025
  • 9
    Tokyo Electron and IBM Renew Collaboration for Advanced Semiconductor Technology
    • April 2, 2025
  • 10
    IBM Accelerates Momentum in the as a Service Space with Growing Portfolio of Tools Simplifying Infrastructure Management
    • March 27, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • 1
    Tariffs, Trump, and Other Things That Start With T – They’re Not The Problem, It’s How We Use Them
    • March 25, 2025
  • 2
    IBM contributes key open-source projects to Linux Foundation to advance AI community participation
    • March 22, 2025
  • 3
    Co-op mode: New partners driving the future of gaming with AI
    • March 22, 2025
  • 4
    Mitsubishi Motors Canada Launches AI-Powered “Intelligent Companion” to Transform the 2025 Outlander Buying Experience
    • March 10, 2025
  • PiPiPi 5
    The Unexpected Pi-Fect Deals This March 14
    • March 13, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.