This document introduces Rust and provides an overview of its key concepts. It begins with an introduction to the presenter and agenda. It then covers basic terminology, common system programming errors, why Rust was created, how to install Rust, data types including primitive types, complex data structures, ownership and borrowing rules, lifetimes, and how to get involved in the Rust community. Key concepts discussed include Rust's type system, memory safety features, and package manager.
Viki::About();
- Lives inBengaluru, India
- Works at MuSigma Research
- Mozilla Representative in India
- Developing DeepRust Crate
- Author of Rust CookBook by Packt
- Mozilla TechSpeaker Program member
3.
Agenda
1. Basic Terminologies
2.Common System programming bugs
3. Why Rust?
4. Intro to Rust
5. Type System
6. Ownership and Borrowing
7. Getting started with Rust community
8. ???
4.
Basic Terminologies
● Lowand high level language
● System programming
● Stack and heap
● Concurrency and parallelism
● Compile time and run time
● Type system
● Garbage collector
● Mutability
● Scope
Why do weneed a new system programming
language?
● State or art programming language
● Solves a lot of common system programming bugs
● Cargo : Rust Package manager
● Improving your toolkit
● Self learning
● It's FUN ...
10.
Rust
● System programminglanguage
● Has great control like C/C++
● Safety and expressive like python
11.
Best things aboutRust
● Strong type system
○ Reduces a lot of common bugs
● Borrowing and Ownership
○ Memory safety
○ Freedom from data races
● Zero Cost abstraction
12.
Installing Rust
# Ubuntu/ MacOS
● Open your terminal (cntrl + Alt +T)
● curl -sSf https://static.rust-lang.org/rustup.sh | sh
13.
Installing Rust
rustc --version
cargo--version
# Windows
● Go to https://win.rustup.rs/
○ This will download rustup-init.exe
● Double click and start the installation
Tuples
// Declaring atuple
let rand_tuple = ("Mozilla Science Lab", 2016);
let rand_tuple2 : (&str, i8) = ("Viki",4);
// tuple operations
println!(" Name : {}", rand_tuple2.0);
println!(" Lucky no : {}", rand_tuple2.1);
26.
Arrays
let rand_array =[1,2,3]; // Defining an array
println!("random array {:?}",rand_array );
println!("random array 1st element {}",rand_array[0] ); // indexing starts with 0
println!("random array length {}",rand_array.len() );
println!("random array {:?}",&rand_array[1..3] ); // last two elements
27.
String
let rand_string ="I love Mozilla Science <3"; // declaring a random string
println!("length of the string is {}",rand_string.len() ); // printing the length of the
string
let (first,second) = rand_string.split_at(7); // Splits in string
let count = rand_string.chars().count(); // Count using iterator count
Borrowing
If you haveaccess to a value in Rust, you can lend out that access to the
functions you call
40.
Types of Borrowing
Thereis two type of borrowing in Rust, both the cases aliasing and mutation do
not happen simultaneously
● Shared Borrowing (&T)
● Mutable Borrow (&mut T)
41.
&mut T
fn add_one(v:&mut Vec<u32> ) {
v.push(1)
}
fn foo() {
let mut v = Vec![1,2,3];
add_one(&mut v);
}
42.
Rules of Borrowing
●Mutable borrows are exclusive
● Cannot outlive the object being borrowed
43.
Cannot outlive theobject being borrowed
fn foo{
let mut v = vec![1,2,3];
let borrow1 = &v;
let borrow2 = &v;
add_one(&mut v): // ERROR : cannot borrow ‘v’ as mutuable because
} it is also borrowed as immutable
Getting started withRust community
● Follow all the latest news at Reddit Channel
○ https://www.reddit.com/r/rust/
● Have doubts, post in
○ https://users.rust-lang.org
○ #rust IRC channel
● Want to publish a crate,
○ https://crates.io
● Follow @rustlang in twitter,
○ https://twitter.com/rustlang
● Subscribe to https://this-week-in-rust.org/ newsletter
46.
Getting started withRust community
● Create your rustaceans profile,
○ Fork https://github.com/nrc/rustaceans.org
○ Create a file in data directory with <github_id>.json
■ Ex: dvigneshwer.json