Giter VIP home page Giter VIP logo

rust-demo's Introduction

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/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.