SlideShare a Scribd company logo
1 of 18
Download to read offline
Rust
Fernando Borretti
What is it?
A new programming language from Mozilla.
Memory safety without garbage collection.
Performance matching that of C.
Safe concurrency.
Without further ado
fn main() {
println!("Hello, world!");
}
$ rustc hello.rs
$ ./hello
Hello, world!
Memory Safety
Rust constrains where and when you can dereference pointers.
The compiler can prove where to insert calls to free().
No dereferencing NULL pointers, no double free() errors, or buffer
overflows.
Example: Double Free
In C, this cuases a double free error:
#include <stdlib.h>
int main() {
int* ptr = malloc(sizeof(int));
int* ref = ptr;
free(ptr);
free(ref);
}
This is a contrived example, but double free errors happen often,
e.g. freeing a recursive data structure.
Example: Double Free
In Rust:
fn main() {
let ptr: Box<i32> = box 10;
let r = ptr;
}
No explicit free(), the compiler figures out where to free the memory.
Ownership
Note, however, we can’t use both pointers at the same time. The
following causes an error:
fn main() {
let ptr: Box<i32> = box 10;
let r = ptr;
*ptr = 11;
}
Rust pointers are not raw numbers like in C, rather, they are like
std::unique_ptr or std::auto_ptr.
When pointer r is assigned the value of pointer ptr, ptr becomes invalid
for the rest of the lifetime of r.
Algebraic Data Types
ADTs are like classes, but in reverse.
Instead of having a base class, and classes that inherit from that one, etc.,
you have a single type that can be any one of different variants.
Algebraic Data Types
Instead of this:
class Geometry {};
class Square : Geometry {
double side;
public:
Square(double s): side(s) {};
};
class Rectangle : Geometry {
double length, width;
public:
Rectangle(double l, double w): length(l), width(w) {};
};
int main() {
Square sq = Square(10.0);
Rectangle rect = Rectangle(1.2, 3.2);
}
Algebraic Data Types
You have this:
enum Geometry {
Square(f64),
Rectangle(f64, f64)
}
fn main() {
let sq = Geometry::Square(10.0);
let rect = Geometry::Rectangle(1.2, 3.2);
}
Option Types
Defined like this:
enum Option<T> {
Some(T),
None,
}
Used like this:
fn safe_division(dividend: int, divisor: int) -> Option<int> {
if divisor == 0 {
None
} else {
Some(dividend / divisor)
}
}
Option types are essentially like having a NULL-able value, only unlike most
type systems, you get to choose which types are nullable.
Pattern Matching
Like if isinstance(obj, type), but better.
Simple Matching
You can match values
fn print_int(x: i64) {
match x {
1 => println!("one");
2 => println!("two");
_ => println!("Not implemented :C");
}
}
And ranges and expressions:
match num {
1 | 2 => ...;
3 ... 5 => ...;
_ => ...;
}
More Complex Matching
And other constructs, like the Option type:
let div = safe_division(1, 0);
match div {
Some(q) => ...; // Do something with the quotient
None => ...; // Divisor was 0
}
Macros
Macros provide source-to-source transformation.
You define a macro to match a particular pattern of source code, and
transform it into another.
Patterns can include variables.
Example: Macros
For example, let’s make a macro for constructing linked lists.
You have a list like this:
enum List {
Cons(i64, Box<List>),
Nil
}
So we write this recursive macro:
macro_rules! make_list (
() => {
List::Nil
};
($first:expr $(, $rest:expr)*) => {
List::Cons($first, box make_list!($($rest),*));
};
)
Example Source
So now, this:
let list = make_list!(1, 2, 3);
Expands to this:
let list =
List::Cons(1,
box() List::Cons(2,
box() List::Cons(3,
box() List::Nil)));
Current Status
Asymptotically approaching 1.0.

More Related Content

What's hot

What's hot (20)

3. basic data structures(2)
3. basic data structures(2)3. basic data structures(2)
3. basic data structures(2)
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
String handling
String handlingString handling
String handling
 
Linked list
Linked listLinked list
Linked list
 
Files
FilesFiles
Files
 
Dma
DmaDma
Dma
 
16 dynamic-memory-allocation
16 dynamic-memory-allocation16 dynamic-memory-allocation
16 dynamic-memory-allocation
 
LZ78
LZ78LZ78
LZ78
 
Lz77 (sliding window)
Lz77 (sliding window)Lz77 (sliding window)
Lz77 (sliding window)
 
Introduction to Iteratees (Scala)
Introduction to Iteratees (Scala)Introduction to Iteratees (Scala)
Introduction to Iteratees (Scala)
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
Chapter16 pointer
Chapter16 pointerChapter16 pointer
Chapter16 pointer
 
Intoduction to dynamic memory allocation
Intoduction to dynamic memory allocationIntoduction to dynamic memory allocation
Intoduction to dynamic memory allocation
 
Maintainable go
Maintainable goMaintainable go
Maintainable go
 
CLinkedList
CLinkedListCLinkedList
CLinkedList
 
10 simulation
10 simulation10 simulation
10 simulation
 
Cyclcone a safe dialect of C
Cyclcone a safe dialect of CCyclcone a safe dialect of C
Cyclcone a safe dialect of C
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
 
Multi prefix trie
Multi prefix trieMulti prefix trie
Multi prefix trie
 

Viewers also liked

Go Programming Language by Google
Go Programming Language by GoogleGo Programming Language by Google
Go Programming Language by GoogleUttam Gandhi
 
Introduction to Go programming language
Introduction to Go programming languageIntroduction to Go programming language
Introduction to Go programming languageSlawomir Dorzak
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010JUG Lausanne
 
[112] 실전 스위프트 프로그래밍
[112] 실전 스위프트 프로그래밍[112] 실전 스위프트 프로그래밍
[112] 실전 스위프트 프로그래밍NAVER D2
 
[132] rust
[132] rust[132] rust
[132] rustNAVER D2
 
Sahara India Finance Double Your Investment in 5 yrs and 11 months contact 92...
Sahara India Finance Double Your Investment in 5 yrs and 11 months contact 92...Sahara India Finance Double Your Investment in 5 yrs and 11 months contact 92...
Sahara India Finance Double Your Investment in 5 yrs and 11 months contact 92...Vikaas Dutta
 

Viewers also liked (6)

Go Programming Language by Google
Go Programming Language by GoogleGo Programming Language by Google
Go Programming Language by Google
 
Introduction to Go programming language
Introduction to Go programming languageIntroduction to Go programming language
Introduction to Go programming language
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010
 
[112] 실전 스위프트 프로그래밍
[112] 실전 스위프트 프로그래밍[112] 실전 스위프트 프로그래밍
[112] 실전 스위프트 프로그래밍
 
[132] rust
[132] rust[132] rust
[132] rust
 
Sahara India Finance Double Your Investment in 5 yrs and 11 months contact 92...
Sahara India Finance Double Your Investment in 5 yrs and 11 months contact 92...Sahara India Finance Double Your Investment in 5 yrs and 11 months contact 92...
Sahara India Finance Double Your Investment in 5 yrs and 11 months contact 92...
 

Similar to Rust programming language overview

Similar to Rust programming language overview (20)

C# programming
C# programming C# programming
C# programming
 
Briefly Rust
Briefly RustBriefly Rust
Briefly Rust
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
C tutorial
C tutorialC tutorial
C tutorial
 
Testing for share
Testing for share Testing for share
Testing for share
 
Lezione03
Lezione03Lezione03
Lezione03
 
Lezione03
Lezione03Lezione03
Lezione03
 
Clanguage
ClanguageClanguage
Clanguage
 
Embedded C - Lecture 2
Embedded C - Lecture 2Embedded C - Lecture 2
Embedded C - Lecture 2
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
 
C
CC
C
 
C
CC
C
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Java 5 New Feature
Java 5 New FeatureJava 5 New Feature
Java 5 New Feature
 
Os Vanrossum
Os VanrossumOs Vanrossum
Os Vanrossum
 
C language introduction
C language introduction C language introduction
C language introduction
 
C language
C languageC language
C language
 
Pointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptxPointers and Memory Allocation ESC101.pptx
Pointers and Memory Allocation ESC101.pptx
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 

Recently uploaded (20)

Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 

Rust programming language overview

  • 2. What is it? A new programming language from Mozilla. Memory safety without garbage collection. Performance matching that of C. Safe concurrency.
  • 3. Without further ado fn main() { println!("Hello, world!"); } $ rustc hello.rs $ ./hello Hello, world!
  • 4. Memory Safety Rust constrains where and when you can dereference pointers. The compiler can prove where to insert calls to free(). No dereferencing NULL pointers, no double free() errors, or buffer overflows.
  • 5. Example: Double Free In C, this cuases a double free error: #include <stdlib.h> int main() { int* ptr = malloc(sizeof(int)); int* ref = ptr; free(ptr); free(ref); } This is a contrived example, but double free errors happen often, e.g. freeing a recursive data structure.
  • 6. Example: Double Free In Rust: fn main() { let ptr: Box<i32> = box 10; let r = ptr; } No explicit free(), the compiler figures out where to free the memory.
  • 7. Ownership Note, however, we can’t use both pointers at the same time. The following causes an error: fn main() { let ptr: Box<i32> = box 10; let r = ptr; *ptr = 11; } Rust pointers are not raw numbers like in C, rather, they are like std::unique_ptr or std::auto_ptr. When pointer r is assigned the value of pointer ptr, ptr becomes invalid for the rest of the lifetime of r.
  • 8. Algebraic Data Types ADTs are like classes, but in reverse. Instead of having a base class, and classes that inherit from that one, etc., you have a single type that can be any one of different variants.
  • 9. Algebraic Data Types Instead of this: class Geometry {}; class Square : Geometry { double side; public: Square(double s): side(s) {}; }; class Rectangle : Geometry { double length, width; public: Rectangle(double l, double w): length(l), width(w) {}; }; int main() { Square sq = Square(10.0); Rectangle rect = Rectangle(1.2, 3.2); }
  • 10. Algebraic Data Types You have this: enum Geometry { Square(f64), Rectangle(f64, f64) } fn main() { let sq = Geometry::Square(10.0); let rect = Geometry::Rectangle(1.2, 3.2); }
  • 11. Option Types Defined like this: enum Option<T> { Some(T), None, } Used like this: fn safe_division(dividend: int, divisor: int) -> Option<int> { if divisor == 0 { None } else { Some(dividend / divisor) } } Option types are essentially like having a NULL-able value, only unlike most type systems, you get to choose which types are nullable.
  • 12. Pattern Matching Like if isinstance(obj, type), but better.
  • 13. Simple Matching You can match values fn print_int(x: i64) { match x { 1 => println!("one"); 2 => println!("two"); _ => println!("Not implemented :C"); } } And ranges and expressions: match num { 1 | 2 => ...; 3 ... 5 => ...; _ => ...; }
  • 14. More Complex Matching And other constructs, like the Option type: let div = safe_division(1, 0); match div { Some(q) => ...; // Do something with the quotient None => ...; // Divisor was 0 }
  • 15. Macros Macros provide source-to-source transformation. You define a macro to match a particular pattern of source code, and transform it into another. Patterns can include variables.
  • 16. Example: Macros For example, let’s make a macro for constructing linked lists. You have a list like this: enum List { Cons(i64, Box<List>), Nil } So we write this recursive macro: macro_rules! make_list ( () => { List::Nil }; ($first:expr $(, $rest:expr)*) => { List::Cons($first, box make_list!($($rest),*)); }; )
  • 17. Example Source So now, this: let list = make_list!(1, 2, 3); Expands to this: let list = List::Cons(1, box() List::Cons(2, box() List::Cons(3, box() List::Nil)));