Rust is the future of systems programming

  1. Why?
  2. C++ vs Rust
  3. Pros and Cons of Rust usage

Syllabus

Microsoft report graph

Why?

Application binary interface

  • Each platform has it's own compiler
  • ABI is not compatible (even with compiler versions)

Language Complexity

Language Complexity

SFINAE

Exeptions

C++ vs Rust

By examples

std::vector<std::string> items{"1", "2", "3", "4", "5"};
auto & last = items.at(4);
items.push_back("6");
last[0] = 'o';

for (const auto & item : items)
{
	std::cout << item << ' ';
}
gcc -o sample -x c++ -O3 -Wall -Werror main.cpp -lstdc++ && ./sample
std::vector<std::string> items{"1", "2", "3", "4", "5"};
auto & last = items.at(4);
items.push_back("6");
last[0] = 'o';

for (const auto & item : items)
{
	std::cout << item << ' ';
}

C++

fn main() {
    let mut items = vec![String::from("1"), String::from("2"),
                         String::from("3"), String::from("4"),
                         String::from("5")];

    let last = &mut items[4];
    // items.push(String::from("8"));
    *last = String::from("o");

    for item in &items {
        print!("{} ", item);
    }
}

Rust

Rust

fn main() {
    let mut items = vec![String::from("1"), String::from("2"),
                         String::from("3"), String::from("4"),
                         String::from("5")];

    let last = &mut items[4];
    items.push(String::from("8"));
    *last = String::from("o");

    for item in &items {
        print!("{} ", item);
    }
}

Borrow checker

fn main() {
    let mut x = 5u32;
    let ref1 = &x;
    let ref2 = &x;
    let ref3 = &mut x;
    *ref3 += 1u32;
    println!("{}", ref3);
    println!("{}", ref1);
    println!("{}", ref2);
}

Unsafe Rust

fn main() {
    let raw_p: *const u32 = &10;

    unsafe {
        assert!(*raw_p == 10);
    }
}

Rust isn't complex(hah)

fn main() {
    // allocate on heap
    let value_on_heap = Box::new(String::from("---")); // C++ unique pointer
    let shared_pointer = Rc::new(String::from("----"));
	// everything is moved by default
    for i in 0..=10 {
        println!("{}", i);

        let x = if i % 2 == 0 {
            println!("Even");
            5
        } else {
        	0
        };
    }
}
// the keyword fn is how we know it is a function
pub fn say_hello(name: &str) -> String { // use the -> to say what type the function returns
  let message = format!("hello, {}!", name);
  message // return a value from the last line of a function by omitting the semicolon
}

Performance and Binary size

Tooling

  • Library distribution
  • Package managers
  • Build systems
  • Build system generators(Cmake, GN)

Rust Tooling

Cargo is Rust’s build system and package manager

// create new project
$ cargo new hello_cargo
$ cd hello_cargo

File Cargo.toml
[package]
name = "hello_cargo"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]
edition = "2018"

[dependencies]
rand = "0.3.1"

// if we want to build project
// and download dependencies
$ cargo build

C++ ecosystem

Rust ecosystem

Pros and Cons of Rust usage

Pros

  • Great tooling
  • Language safety
  • Easy to start new projects
  • Features of language
  • Safe Threading

Cons

  • No method overload
  • No inheritance (to discuss)
  • Not so mature ecosystem(depends on what you are doing)

Questions?

Rust is the future of systems programming

By Silvestr Predko

Rust is the future of systems programming

  • 318