Let’s Get Rusty
License: CC BY-SA 4.0 International. You are free to share and adapt.
Nov 23, 2018 | Rust Community Meet-up 2018 | Delhi NCR
Abhiram Ravikumar | Mozilla Tech Speaker | ML Research Fellow, SAP Labs India
@abhi12ravi
Hi there!
Abhiram Ravikumar
• Mozilla Tech Speaker
• Open Source Evangelist
• Data Science Research Fellow
• Lynda Instructor
• Software Developer, SAP Labs
India
Questions / Feedback - @abhi12ravi
Agenda
• Why Rust?
• Rust features – (what have you?)
• FFIs (Foreign Function Interfaces) demo
• Rust Uses and Applications
@abhi12ravi
The audience - Orgs
@abhi12ravi
The audience – Rust knowledge
@abhi12ravi
The audience – rusty-projects
@abhi12ravi
Prelude: C++ Problems
• Dangling pointer
• Segmentation fault
• Iterator invalidation
• Thread safety
int* abc() {
int x = 1;
return &x;
}
@abhi12ravi
Prelude: C++ Problems
• Dangling pointer
• Segmentation fault
• Iterator invalidation
• Thread safety
int* abc() {
int x = 1;
return &x;
}
@abhi12ravi
Systems level
Memory safe
Thread safe
Fast ⚡
OOPS
Strong Compiler
FFIs
No GC!
Memory management – The Good Stuff
• Concept of mutability
• Ownership &
Borrowing
Concept of mutability – the why
• Variables are immutable by
default
• Add keyword mut to make it
mutable
• Future readers know the value is
going to change
• Less prone to unwanted changes
in the variable’s value
fn main() {
let mut x = 5;
println!(“X is {}“, x);
x = 6;
println!(“X is {}“, x);
}
// X is 5
// X is 6
Memory allocation - Fundamentals
1. The memory must be requested from the operating
system at runtime.
2. We need to return this memory to the operating system
when we’re done with our usage.
Returning memory to the OS
Other programming languages:
• Garbage Collector (GC)
• W/o GC – programmer’s responsibility
Challenges:
• Need to pair exactly one allocate with exactly one free
In Rust
{
let s = String::from("hello"); // s is valid from this point forward
// do stuff with s
} // this scope is now over, and s is no longer valid
Memory is automatically returned once variable goes out of
scope.
Also, in other news…
This week in Rust…
News & Blog Posts
The relative performance of C and Rust
Quote of the Week
Bare Metal Attracts Rust
- Sven Gregori on
Hackaday
Enough Rust, let’s switch to
Python!
Python Python
• Strong Ecosystem
• Fast iterations
• Robust interpreter
• Powerful meta programming
(APIs)
• Library support - fantastic
• Really slow
• Performance issues
• Far from metal
• Poor multi-threading
support
Rust Python
• Speed – fast ⚡
• Compile time ⬇️
• Stable environment (Reliability)
• Crash handling functionality ✔️
• Closer to metal
• Really slow
• Performance issues
• Far from the metal
• Poor multi-threading
support
➕ =
💚
The Bottomline…
⚡
Credits: @rochacbruno on
GitHub
Demos
Sample Problem Statement
String Processing use-case
• Count the pairs of repeated characters in the given string
• How many subsequent-repeated groups of chars are in the
given string?
The performance figures
Performance Benchmark
Scenario pure_python python_with_rus
t
Observation
Mean (ms) 53.9732 2.6085 ~ 20x faster
Max (ms) 56.9428 2.9296 ~19x faster
“Innovation is a necessity now,
and the faster your programs run,
the faster you innovate!”
Abhiram Ravikumar
References
1. The Rust book
2. Rust Python Example by Rocha C Bruno
3. Armin Ronacher: A Python and Rust love story
4. Dan Callahan - My Python's a little Rust-y - PyCon 2015
5. Extending Python with Rust (Samuel Cormier-Iijima)
6. All you need to know about FFI
Thank you!
This slide deck is available here:
speakerdeck.com/abhi12ravi

Rust meetup delhi nov 18

  • 1.
    Let’s Get Rusty License:CC BY-SA 4.0 International. You are free to share and adapt. Nov 23, 2018 | Rust Community Meet-up 2018 | Delhi NCR Abhiram Ravikumar | Mozilla Tech Speaker | ML Research Fellow, SAP Labs India @abhi12ravi
  • 2.
    Hi there! Abhiram Ravikumar •Mozilla Tech Speaker • Open Source Evangelist • Data Science Research Fellow • Lynda Instructor • Software Developer, SAP Labs India Questions / Feedback - @abhi12ravi
  • 3.
    Agenda • Why Rust? •Rust features – (what have you?) • FFIs (Foreign Function Interfaces) demo • Rust Uses and Applications @abhi12ravi
  • 4.
    The audience -Orgs @abhi12ravi
  • 5.
    The audience –Rust knowledge @abhi12ravi
  • 6.
    The audience –rusty-projects @abhi12ravi
  • 7.
    Prelude: C++ Problems •Dangling pointer • Segmentation fault • Iterator invalidation • Thread safety int* abc() { int x = 1; return &x; } @abhi12ravi
  • 8.
    Prelude: C++ Problems •Dangling pointer • Segmentation fault • Iterator invalidation • Thread safety int* abc() { int x = 1; return &x; } @abhi12ravi
  • 10.
    Systems level Memory safe Threadsafe Fast ⚡ OOPS Strong Compiler FFIs No GC!
  • 11.
    Memory management –The Good Stuff • Concept of mutability • Ownership & Borrowing
  • 12.
    Concept of mutability– the why • Variables are immutable by default • Add keyword mut to make it mutable • Future readers know the value is going to change • Less prone to unwanted changes in the variable’s value fn main() { let mut x = 5; println!(“X is {}“, x); x = 6; println!(“X is {}“, x); } // X is 5 // X is 6
  • 13.
    Memory allocation -Fundamentals 1. The memory must be requested from the operating system at runtime. 2. We need to return this memory to the operating system when we’re done with our usage.
  • 14.
    Returning memory tothe OS Other programming languages: • Garbage Collector (GC) • W/o GC – programmer’s responsibility Challenges: • Need to pair exactly one allocate with exactly one free
  • 15.
    In Rust { let s= String::from("hello"); // s is valid from this point forward // do stuff with s } // this scope is now over, and s is no longer valid Memory is automatically returned once variable goes out of scope.
  • 16.
  • 17.
    This week inRust… News & Blog Posts The relative performance of C and Rust Quote of the Week Bare Metal Attracts Rust - Sven Gregori on Hackaday
  • 18.
    Enough Rust, let’sswitch to Python!
  • 19.
    Python Python • StrongEcosystem • Fast iterations • Robust interpreter • Powerful meta programming (APIs) • Library support - fantastic • Really slow • Performance issues • Far from metal • Poor multi-threading support
  • 20.
    Rust Python • Speed– fast ⚡ • Compile time ⬇️ • Stable environment (Reliability) • Crash handling functionality ✔️ • Closer to metal • Really slow • Performance issues • Far from the metal • Poor multi-threading support
  • 21.
  • 22.
  • 23.
    Sample Problem Statement StringProcessing use-case • Count the pairs of repeated characters in the given string • How many subsequent-repeated groups of chars are in the given string?
  • 24.
  • 25.
    Performance Benchmark Scenario pure_pythonpython_with_rus t Observation Mean (ms) 53.9732 2.6085 ~ 20x faster Max (ms) 56.9428 2.9296 ~19x faster
  • 26.
    “Innovation is anecessity now, and the faster your programs run, the faster you innovate!” Abhiram Ravikumar
  • 27.
    References 1. The Rustbook 2. Rust Python Example by Rocha C Bruno 3. Armin Ronacher: A Python and Rust love story 4. Dan Callahan - My Python's a little Rust-y - PyCon 2015 5. Extending Python with Rust (Samuel Cormier-Iijima) 6. All you need to know about FFI
  • 28.
    Thank you! This slidedeck is available here: speakerdeck.com/abhi12ravi