Rust (programming language): Difference between revisions

From David's Wiki
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
Rust is a low-level programming language. It's main advantage is supposed to be memory safety by default.   
Rust is a low-level programming language.
Recently, Rust has been adopted to many systems applications such as the Linux kernel drivers and Android.
The main advantage is that it encourages memory-safe programming through reference ownership and by isolating memory-unsafe functions.   


==Usage==
==Usage==
Line 10: Line 10:


==Syntax==
==Syntax==
===Example===
===Basics===
<syntaxhighlight lang="rust">
<syntaxhighlight lang="rust">
# Const variable, must have type annotation.
// Const variable, must have type annotation.
const IMPORTANT_VALUE: i32 = 50;
const IMPORTANT_VALUE: i32 = 50;
fn main() {
fn main() {
   # C++: int x = 3;
   // C++: int x = 3;
   let mut x: i32 = 3;
   let mut x: i32 = 3;


   # Shadowing, creating a new variable.
   // Shadowing, creating a new variable.
   let x = x + 5;
   let x = x + 5;
 
 
   # Loop over values [0, 1, 2]
   // Loop over values [0, 1, 2]
   for i in 0..3 {
   for i in 0..3 {
     println!("This number is {}", i);
     println!("This number is {}", i);
   }
   }
 
 
   # Ternary is a single line if statement.
   // Ternary is a single line if statement.
   let big_x = if x > 5 {x} else {5};
   let big_x = if x > 5 {x} else {5};
}
}


fn lerp(a: f64, b: f64, x: f64) -> f64 {
fn lerp(a: f64, b: f64, x: f64) -> f64 {
   # No semicolon implies return.
   // No semicolon implies return.
   (1.0 - x) * a + x * b
   (1.0 - x) * a + x * b
}
}


# Copied from rustlings.
// Copied from rustlings.
pub fn fizz_if_foo(fizzish: &str) -> &str {
pub fn fizz_if_foo(fizzish: &str) -> &str {
     if fizzish == "fizz" {
     if fizzish == "fizz" {
Line 45: Line 45:
     }
     }
}
}
</syntaxhighlight>
===Borrowing===
This is like references or <code>unique_ptr</code> in C++.
<syntaxhighlight lang="rust">
let x = 5;
let mut y = 6;
// C++: const int &
example_borrow(&x)
// C++: int&
example_mut_borrow(&mut y)
</syntaxhighlight>
</syntaxhighlight>



Latest revision as of 15:28, 30 January 2023

Rust is a low-level programming language.
The main advantage is that it encourages memory-safe programming through reference ownership and by isolating memory-unsafe functions.

Usage

Installation

See Install

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

Syntax

Basics

// Const variable, must have type annotation.
const IMPORTANT_VALUE: i32 = 50;
fn main() {
  // C++: int x = 3;
  let mut x: i32 = 3;

  // Shadowing, creating a new variable.
  let x = x + 5;

  // Loop over values [0, 1, 2]
  for i in 0..3 {
    println!("This number is {}", i);
  }

  // Ternary is a single line if statement.
  let big_x = if x > 5 {x} else {5};
}

fn lerp(a: f64, b: f64, x: f64) -> f64 {
   // No semicolon implies return.
  (1.0 - x) * a + x * b
}

// Copied from rustlings.
pub fn fizz_if_foo(fizzish: &str) -> &str {
    if fizzish == "fizz" {
        "foo"
    } else if fizzish == "fuzz" {
        "bar"
    } else {
        "baz"
    }
}

Borrowing

This is like references or unique_ptr in C++.

let x = 5;
let mut y = 6;

// C++: const int &
example_borrow(&x)
// C++: int&
example_mut_borrow(&mut y)

Resources