-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReach_Further_With_Rust.rst
147 lines (110 loc) · 3.16 KB
/
Reach_Further_With_Rust.rst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
Rust's Standard Library
=============================
Reaching Further with Rust
---------------------------
High-level code,low-level performance
Eliminate tradeoff between control and flexibility.
Buffer overflows,Double free,Dangling POinters, Data races
Static type system = Eat your spinach!
.. code:: rust
extern "C" fn fast_blank(buf: Buf) -> bool {
buf.as_slice().chars().all(|c| c.is_whitespace())
}
// High-level, zero-cost abstractions
fn is_whitespace(text: &str) -> bool {
text.chars().all(|c| c.is_whitespace())
}
fn load_images(paths: &[PathBuf]) -> Vec<Image> {
paths.par_iter()
.map(|path| Image::load(path))
.collect()
}
Design of Rust
================
Mozilla developers faced issues in multithreading like
- Sharing memory
- Mutation
- No ordering
Or more collectively **Data Race**
Sharing and Mutation use is mutually exclusive enforced by the concept of **Ownership and Borrowing**
.. code:: rust
// The function Main owns the apple
fn main(){
let apple=Apple::new();
//Give Ownership of the apple
eat(apple);
eat(apple);//fails error as apple has already been moved
}
// Takes ownership of apple
fn eat(apple:Apple){
println!("Eaten {:?}",apple);
}
fn main(){
let apple=Apple::new();
let mut bag=Vec::new();
bag.push(apple);
bag.push(Apple::new());
deliver(bag);
// Share or Loan out bag
let weight=weigh(&bag);
}
fn deliver(bag: Vec<Apple>){
//.....
//when final owner returns value is freed
}
fn weigh(bag: Vec<Apple>) -> u32{
//.....
}
Borrowed values are immutable
After last use of borrowed value is used mutability is restored
Mutable References
***********************
.. code:: rust
let mut bag=Vec::new();
bag.push(...); // bag mutable;
let r= &mut bag; //bag mutably borrowed
bag.len();// Cannot access bag while borrowed
r.push(..); //but can mutate through r
bag.push(...); //at last bagg is accessible again
Parallellism
**************
Building parallel abstractions is easy
Misusing those abstractions is also easy
.. code:: rust
fn foo(...){
let m=HashMap::new();
m.insert("Hello","World");
channel.send(m);
m.insert("Hello","Data Race!"); //error.use of moved value
}
//Method
impl<T> Channel<T>{
fn send(&mut self,data: T){ //Take Ownership of data
...
}
}
Concurrency Paradigms
**********************
========= ================ ================
Paradigm Ownership? Borrowing?
========= =============== ================
Message Parsing YES NO
Fork Join NO YES
Rust in Production
===================
Rust Community
==============
Modules in the std library
----------------------------
- The Prelude Module.
List of things that Rust automatically imports into every program
1. Rust provides a powerful macro system that allows metaprogramming
2. Rust Macros are non conflicting
.. code:: rust
#![macro_use]
#[macro export]
macro_rules! welcome{
()=>(
println!("Welcome to Rust Macros");
)
}