Skip to content

Hands-on demo of the Rust programming language.

License

Notifications You must be signed in to change notification settings

dev-cafe/rust-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rust-demo

Hands-on demo of the Rust programming language.

History of Rust

Why Rust

  • "A language empowering everyone to build reliable and efficient software." (https://www.rust-lang.org)
  • Fast but also safe
  • Zero cost abstractions
  • Type system
  • Compiler catches most errors (if it compiles, it often just works)
  • Compiler provides helpful error messages
  • Memory safety
  • Thread safety
  • Private and immutable by default
  • Great tooling (testing, documentation, auto-formatter, dependency management, package registry, no makefiles needed)
  • Community
  • Most-loved programming language in the 2016, 2017, 2018, 2019, and 2020 Stack Overflow annual surveys

Great resources

Memory model

  • No explicit allocation and deallocation.
  • No garbage collector either.
  • Each value in Rust has an owner and there can only be one owner at a time.
  • When the owner goes out of scope, the value is dropped.
  • Rust knows the size of all stack allocations at compile time.

This does not compile:

let s1 = String::from("hello");
let s2 = s1;

println!("{}, world!", s1);

Installing Rust

Preferred way to install Rust for most developers: https://www.rust-lang.org/tools/install

Verify your installations of cargo and rustc:

$ cargo --version
cargo 1.47.0 (f3c7e066a 2020-08-28)

$ rustc --version
rustc 1.47.0 (18bf6b4f0 2020-10-07)

Hands-on demo

In this demo we will approximate pi by generating random points and computing their distance to origin:1

random points

Tasks/discussion points:

  • Show how to bootstrap a project with cargo new
  • Step out of the newly bootstrapped project
  • Clone the code (this repository)
  • Browse and discuss the sources
  • Compile the sources with cargo check
  • Compare cargo check and cargo build
  • Discuss cargo run
  • Experiment with cargo fmt
  • Run the tests with cargo test
  • Generate optimized version with cargo build --release
  • Generate the documentation with cargo doc
  • Discuss cargo publish and https://crates.io (the Rust package registry)

Interfacing with C, C++, Fortran, and Python

C calling Rust and Fortran calling Rust

cargo build --release

gfortran -L target/release/ -lpi examples/fortran/example.f90 -o fortran-example.x
gcc -L target/release/ -lpi examples/c/example.c -o c-example.x

env LD_LIBRARY_PATH=target/release/ ./fortran-example.x
env LD_LIBRARY_PATH=target/release/ ./c-example.x

Python calling Rust

Using PyO3 and Maturin:

cargo build --release
maturin develop --release
python -c "import pi; print(pi.pi_approximation(1000000))"

1: GIF from https://www.soroushjp.com/2015/02/07/go-concurrency-is-not-parallelism-real-world-lessons-with-monte-carlo-simulations/

About

Hands-on demo of the Rust programming language.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages