Professional Documents
Culture Documents
Rust 101 - Everything You Need To Know About Rust - by Nishant A
Rust 101 - Everything You Need To Know About Rust - by Nishant A
Search
Listen Share
72 2
Source
Why Rust?
Rust is a programming language that is built to be fast, secure,
reliable and supposedly easier to program. Rust was, on the
contrary, not built to replace C or C++. It’s aim is completely
different, that is, to be an all-purpose general programming
language.
Features of Rust
1. Code should be able to run without crashing first time — Yes, you
heard me. Rust is a statically typed language, and based on the
way it is constructed, it leaves almost (I say, almost!) no room for
your program to crash.
fn main() {
let myName = "Nishant"; // Rust infers type to be String
let coins: u32 = 4; // explicit type, unsigned 32-bit integer
let age = 19; // number defaults to i32 type, signed 32-bit integer
}
fn foobar(num: u32) {
println!("The number sent was {}", num);
}
For more information about this error, try `rustc --explain E0381`.
error: could not compile `hello_cargo` due to previous error
There are many data types used in Rust. We have come across
String , i32 and u32 . We also have,
fn main() {
let temperature: f32 = 6.4;
let circumference: f64 = 23053.7106;
Those above were some more primitive data types. Rust has
support for compound data types as well!
fn main() {
let pair = ('A', 65);
// Destructuring a pair.
let (letter, number) = pair
}
The implicit type for pair is (char, i32) . Tuples are
heterogeneous and can support nested tuples as well.
fn main() {
let a = [1, 2, 3, 4, 5];
// a has a type [i32; 5] - an array of five signed 32-bit integers.
}
fn main() {
let a = [3; 5];
for i in a {
println!("{i}");
}
}
Functions
We have seen we have been using the main function to denote the
starting point in our program. The syntax of defining functions is
Let Expressions
Combining the knowledge of variables and functions, we can
assign values like this:
let x = {
let y = 1;
let z = 2;
fn main() {
let x = 0;
let x = { 0 }; // these two are the same!
}
fn main () {
let x = 4;
{
let x = "shadowing x";
println!("{}", x); // pfints "shadowing x"
}
Namespaces
If we wish to use functions from other libraries, we can use
namespaces.
fn main() {
let least = std::cmp::min(3, 8);
println!("{}", least);
}
We can also bring the function into scope by using the use
keyword.
use std::cmp::min;
fn main() {
let least = min(3, 8);
println!("{}", least);
}
Structs
Structs are similar to structs in C. We can define a struct
Coordinate as below and initialize a variable of that type.
struct Coordinate {
x: f64,
y: f64
}
fn main() {
let somewhere = Coordinate { x: 23, y: 3.5 };
// Destructuring Coordinate.
let Coordinate {
x,
y
} = elsewhere;
}
Pattern Matching
Pattern Matching is like a conditional structure.
fn main() {
let coordinate = Coordinate { x: 3.0, y: 5.0 }
fn main() {
let coordinate = Coordinate { x: 3.0, y: 5.0 }
match coordinate {
Coordinate { x: 3.0, y } => println!("(3, {})", y);
_ => println!("x != three");
}
}
match coordinate {
Coordinate { x: 3.0, y } => println!("(3, {})", y);
Coordinate { .. } => println!("x != three");
}
}
Traits
Traits are like type classes in Haskell, or interfaces in Java. Say
that we have a struct, Number which looks like this:
struct Number {
value: isize,
prime: bool
}
trait Parity {
fn is_even(self) -> bool
}
Macros
Macros are a part of meta-programming. Marcos can be
considered as little programs that other programs can use. All
macros end with ! and can be defined as either of the following:
macro_name!()
macro_name!{}
macro_name![]
fn main() {
let vec1 = vec![1,2,3];
Enums
Enums in Rust are like sealed class in Kotlin. They are certain
types that we can define in an enclosure. The following example
is coupled with Generics. Option is also defined in the standard
library.
enum Option<T> {
None,
Some(T)
}
impl Option<T> {
unwrap() -> T {
match self {
Self::None -> panic!("unwrap called on an Option None"),
Self::Some -> T,
}
}
}
fn main() {
{
let result = do_something(); // returns result
match result {
Ok(data) => proceed(data),
Err(error) => panic!("There has been an error!"),
}
The reason I have used this code within a scope block is if we wish
to propagate the error up somewhere, we could replace the code
with:
fn main() {
{
let result = do_something(); // returns result
match result {
Ok(data) => proceed(data),
Err(error) => return error,
}
fn main() {
{
let data = do_something()?; // returns result
Rust Programming
View list 1 story
If you wish to read every article from me, consider joining the
Medium
program with this referral link.
Want to connect?
My GitHub profile.
My Portfolio website.
Web Development
Follow
2.1K 27
Christianlauer in CodeX
849 11
Anmol Tomar in CodeX
502 2
Nishant Aanjaney Jalan in CodeX
159 3
29 4
David Goudet
7.2K 77
Lists
General Coding Knowledge
20 stories · 722 saves
11.3K 128
Apivano
197 11
Alexey Inkin
1.3K 27
Jarrod Overson
3.2K 30