Browse Source

more things about rust

master
Julio Biason 6 years ago
parent
commit
7cf0edd334
  1. 506
      porque-rust.html

506
porque-rust.html

@ -63,9 +63,9 @@
font-weight: bold; font-weight: bold;
} }
.semi-opaque { .semi-opaque {
background-color: rgba(0, 0, 0, 0.7); background-color: rgba(0, 0, 0, 0.7);
} }
</style> </style>
</head> </head>
@ -78,33 +78,51 @@
</section> </section>
</section> </section>
<section> <section>
<section> <section>
<p> <img src="_images/avatar-20170726.png" alt="Me" style="float:left;width:200px;" class="no-border">
<a href="https://insights.stackoverflow.com/survey/2019">A languagem mais amada segundo o StackOverflow Survey</a>
<div>
<ul class="empty">
<li>Júlio Biason</li>
<li>https://functional.cafe/@juliobiason</li>
<li>julio.biason@pm.me</li>
<li><a href="http://presentations.juliobiason.net">http://presentations.juliobiason.net</a></li>
</ul>
</div>
</section>
</section>
<p class="fragment">... pelo 4⁰ ano seguido.</p> <section>
</p> <section>
</section> <p>
</section> <a href="https://insights.stackoverflow.com/survey/2019">
A languagem mais amada segundo o StackOverflow
Survey 2019
</a>
<p class="fragment">... pelo 4⁰ ano seguido.</p>
</p>
</section>
</section>
<section> <section>
<section> <section>
<h2>Imutabilidade por Default</h2> <h2>Imutabilidade por Default</h2>
</section> </section>
<section> <section>
<pre><code class="hljs rust" data-trim> <pre><code class="hljs rust" data-trim>
fn main() { fn main() {
let a = 2; let a = 2;
a = 3; a = 3;
println!("{}", a); println!("{}", a);
} }
</code></pre> </code></pre>
</section> </section>
<section> <section>
<pre><code class="hljs" data-trim> <pre><code class="hljs" data-trim>
3 | let a = 2; 3 | let a = 2;
| - | -
| | | |
@ -112,72 +130,72 @@ fn main() {
| help: make this binding mutable: `mut a` | help: make this binding mutable: `mut a`
4 | a = 3; 4 | a = 3;
| ^^^^^ cannot assign twice to immutable variable | ^^^^^ cannot assign twice to immutable variable
</code></pre> </code></pre>
</section> </section>
<section> <section>
<pre><code class="hljs rust" data-trim> <pre><code class="hljs rust" data-trim>
fn main() { fn main() {
let mut a = 2; let mut a = 2;
a = 3; a = 3;
println!("{}", a); println!("{}", a);
} }
</code></pre> </code></pre>
</section> </section>
</section> </section>
<section> <section>
<section> <section>
<h2>Borrow Checker</h2> <h2>Borrow Checker</h2>
</section> </section>
<section> <section>
<pre><code class="hljs rust" data-trim> <pre><code class="hljs rust" data-trim>
a = String::from("hello"); a = String::from("hello");
</code></pre> </code></pre>
</section> </section>
<section> <section>
"Variável <code>a</code> tem o valor <code>"hello"</code>" "Variável <code>a</code> tem o valor <code>"hello"</code>"
</section> </section>
<section> <section>
<div> <div>
"Posição de memória apontada por <code>a</code> tem o valor <code>"hello"</code>" "Posição de memória apontada por <code>a</code> tem o valor <code>"hello"</code>"
</div> </div>
<div class="fragment"> <div class="fragment">
<pre><code> <pre><code>
0x3f5cbf89 = "hello" 0x3f5cbf89 = "hello"
</code></pre> </code></pre>
</div> </div>
<img src="_images/rust-memory.png" alt="" class="fragment"> <img src="_images/rust-memory.png" alt="" class="fragment">
</section> </section>
<section> <section>
<div> <div>
A language that doesn't affect the way you think A language that doesn't affect the way you think
about programming, is not worth knowing. about programming, is not worth knowing.
</div> </div>
<div> <div>
-- Alan Perlis, "ALGOL" -- Alan Perlis, "ALGOL"
</div> </div>
</section> </section>
<section> <section>
<pre><code class="hljs rust" data-trim> <pre><code class="hljs rust" data-trim>
fn main() { fn main() {
let a = String::from("hello"); let a = String::from("hello");
let _b = a; let _b = a;
println!("{}", a) println!("{}", a)
} }
</code></pre> </code></pre>
</section> </section>
<section> <section>
<pre><code> <pre><code>
error[E0382]: borrow of moved value: `a` error[E0382]: borrow of moved value: `a`
--> src/main.rs:5:20 --> src/main.rs:5:20
| |
@ -187,174 +205,202 @@ error[E0382]: borrow of moved value: `a`
| ^ value borrowed here after move | ^ value borrowed here after move
| |
= note: move occurs because `a` has type `std::string::String`, which does not implement the `Copy` trait = note: move occurs because `a` has type `std::string::String`, which does not implement the `Copy` trait
</code></pre> </code></pre>
</section> </section>
<section> <section>
<p>E se eu precisar acessar a variável em mais de um lugar?</p> <p>E se eu precisar acessar a variável em mais de um lugar?</p>
<h3 class="fragment">References</h3> <h3 class="fragment">References</h3>
</section> </section>
<section> <section>
<pre><code class="hljs rust" data-trim> <pre><code class="hljs rust" data-trim>
fn main() { fn main() {
let a = String::from("hello"); let a = String::from("hello");
let _b = &a; let _b = &a;
println!("{}", a) println!("{}", a)
} }
</code></pre> </code></pre>
</section> </section>
<section> <section>
<img src="_images/rust-reference.png" alt=""> <img src="_images/rust-reference.png" alt="">
</section> </section>
<section> <section>
<h3>Regras do Borrow Checker</h3> <h3>Regras do Borrow Checker</h3>
<ul> <p class="fragment">
<li class="fragment">Uma região de memória tem apenas um dono.</li> Uma região de memória tem apenas um dono.
<li class="fragment">Uma região de memória pode ter inifitas referências </p>
<span class="fragment">
desde que elas não durem mais do que o dono. <p class="fragment">
</span> A região é desalocada quando o dono sair de escopo.
</li> </p>
<li class="fragment"> </section>
É possível ter uma referência mutável de uma região de memória
<span class="fragment"> <section>
mas para haver uma referência mutável ela deve ser a <strong>única</strong> <h3>Regras do Borrow Checker</h3>
referência.
</span> <p class="fragment">
</li> Uma região de memória pode ter inifitas referências.
</ul> </p>
</section>
<p class="fragment">
<section> ... desde que elas não durem mais do que o dono.
<img src="_images/dunno.jpg" alt=""> </p>
</section> </section>
<section> <section>
<pre><code class="hljs go" data-trim>presente := Presente { ... } <h3>Regras do Borrow Checker</h3>
<p class="fragment">
É possível ter uma referência mutável de uma região de memória.
</p>
<p class="fragment">
... mas para haver uma referência mutável ela deve ser
a <strong>única</strong> referência.
</p>
</section>
<section>
<img src="_images/dunno.jpg" alt="">
</section>
<section>
<pre><code class="hljs go" data-trim>presente := Presente { ... }
canal &lt;- presente</code></pre> canal &lt;- presente</code></pre>
</section> </section>
<section> <section>
<pre><code class="hljs go" data-trim>presente := Presente { ... } <pre><code class="hljs go" data-trim>presente := Presente { ... }
canal &lt;- presente canal &lt;- presente
presente.abrir()</code></pre> presente.abrir()</code></pre>
</section> </section>
<section> <section>
<a href="https://swift.org/blog/swift-5-exclusivity/">Swift 5 Exclusivity Enforcement</a> <a href="https://swift.org/blog/swift-5-exclusivity/">Swift 5 Exclusivity Enforcement</a>
</section> </section>
</section> </section>
<section> <section>
<section> <section>
<h2>Tipos Algébricos</h2> <h2>Tipos Algébricos</h2>
<p class="fragment">(structs)</p> <p class="fragment">(structs)</p>
</section> </section>
<section>
<h3>struct</h3>
<section> <pre><code class="hljs rust" data-trim>
<h3>enum</h3> struct Present {
<pre><code class="hljs rust" data-trim> package_color: String,
content: String
}
</code></pre>
</section>
<section>
<h3>enum</h3>
<pre><code class="hljs rust" data-trim>
enum IpAddr { enum IpAddr {
V4, V4,
V6 V6
} }
</code></pre> </code></pre>
</section> </section>
<section> <section>
<pre><code class="hljs rust" data-trim> <pre><code class="hljs rust" data-trim>
enum IpAddr { enum IpAddr {
V4(String), V4(String),
V6(String), V6(String),
} }
</code></pre> </code></pre>
</section> </section>
<section> <section>
<pre><code class="hljs rust" data-trim> <pre><code class="hljs rust" data-trim>
let home = IpAddr::V4(String::from("127.0.0.1"); let home = IpAddr::V4(String::from("127.0.0.1");
match home { match home {
V4(address) =&gt; println!("IPv4 addr: {}", address), V4(address) =&gt; println!("IPv4 addr: {}", address),
V6(address) =&gt; println!("Ipv6 addr: {}", address), V6(address) =&gt; println!("Ipv6 addr: {}", address),
} }
</code></pre> </code></pre>
</section> </section>
<section> <section>
<pre><code class="hljs rust" data-trim> <pre><code class="hljs rust" data-trim>
enum Option&lt;T&gt; { enum Option&lt;T&gt; {
Some(T), Some(T),
None None
} }
</code></pre> </code></pre>
</section> </section>
</section> </section>
<section> <section>
<section> <section>
<h2>Error Control</h2> <h2>Error Control</h2>
</section> </section>
<section> <section>
<pre><code class="hljs python" data-trim> <pre><code class="hljs python" data-trim>
try: try:
something() something()
except Exception: except Exception:
pass pass
</code></pre> </code></pre>
</section> </section>
<section> <section>
<pre><code class="hljs java" data-trim> <pre><code class="hljs java" data-trim>
try { try {
something(); something();
} catch (Exception ex) { } catch (Exception ex) {
System.out.println(ex); System.out.println(ex);
} }
</code></pre> </code></pre>
</section> </section>
<section> <section>
<pre><code class="hljs c" data-trim> <pre><code class="hljs c" data-trim>
FILE* f = fopen("someting.txt", "wb"); FILE* f = fopen("someting.txt", "wb");
fprintf(f, "Done!"); fprintf(f, "Done!");
fclose(f); fclose(f);
</code></pre> </code></pre>
</section> </section>
<section> <section>
<div> <div>
Onde o erro foi tratado nisso? Onde o erro foi tratado nisso?
</div> </div>
</section> </section>
<section> <section>
<pre><code class="hljs rust" data-trim> <pre><code class="hljs rust" data-trim>
enum Result&lt;T, E&gt; { enum Result&lt;T, E&gt; {
Ok(T), Ok(T),
Err(E), Err(E),
} }
</code></pre> </code></pre>
</section> </section>
<section> <section>
<pre><code class="hljs rust" data-trim> <pre><code class="hljs rust" data-trim>
File::create("something.txt") match { File::create("something.txt") match {
Ok(fp) =&gt; fp.write_all(b"Hello world"), Ok(fp) =&gt; fp.write_all(b"Hello world"),
Err(err) =&gt; println!("Failure! {}", err), Err(err) =&gt; println!("Failure! {}", err),
} }
</code></pre> </code></pre>
</section> </section>
<section> <section>
<pre><code class="hljs rust" data-trim> <pre><code class="hljs rust" data-trim>
File::create("something.txt") match { File::create("something.txt") match {
Ok(fp) =&gt; fp.write_all(b"Hello world") match { Ok(fp) =&gt; fp.write_all(b"Hello world") match {
Ok(_) =&gt; (), Ok(_) =&gt; (),
@ -362,8 +408,8 @@ File::create("something.txt") match {
} }
Err(err) =&gt; println!("Failure! {}", err), Err(err) =&gt; println!("Failure! {}", err),
} }
</code></pre> </code></pre>
</section> </section>
<section> <section>
<pre><code class="hljs rust" data-trim> <pre><code class="hljs rust" data-trim>
@ -379,24 +425,30 @@ file.write(b"Hello world")?;
OK(()) OK(())
</code></pre> </code></pre>
</section> </section>
</section> </section>
<section> <section>
<section> <section>
<h2>Crazy stuff</h2> <h2>Macros</h2>
</section> </section>
</section>
<section> <section>
<a href="https://medium.com/@shnatsel/how-rusts-standard-library-was-vulnerable-for-years-and-nobody-noticed-aebf0503c3d6">How Rust’s standard library was vulnerable for years and nobody noticed</a> <section>
</section> <h2>Crazy stuff</h2>
</section>
<section> <section>
<a href="https://medium.com/@sgrif/no-the-problem-isnt-bad-coders-ed4347810270">No, the problem isn’t “bad coders”</a> <a href="https://medium.com/@shnatsel/how-rusts-standard-library-was-vulnerable-for-years-and-nobody-noticed-aebf0503c3d6">How Rust’s standard library was vulnerable for years and nobody noticed</a>
</section> </section>
<section> <section>
<img src="_images/rust-issues.png" alt="4.5k issues no Github" class="stretch"> <a href="https://medium.com/@sgrif/no-the-problem-isnt-bad-coders-ed4347810270">No, the problem isn’t “bad coders”</a>
</section> </section>
<section>
<img src="_images/rust-issues.png" alt="4.5k issues no Github" class="stretch">
</section>
<section> <section>
<a href="https://rustup.rs/">rustup</a> <a href="https://rustup.rs/">rustup</a>
@ -413,36 +465,36 @@ OK(())
<small>wasm32-unknown-unknown</small> <small class="fragment">(WebAssembly)</small> <small>wasm32-unknown-unknown</small> <small class="fragment">(WebAssembly)</small>
</div> </div>
</section> </section>
</section> </section>
<section> <section>
<section> <section>
<h2>Bibliotecas</h2> <h2>Bibliotecas</h2>
</section> </section>
<section> <section>
<h3>Rayon</h3> <h3>Rayon</h3>
<pre><code> <pre><code>
fn sum_of_squares(input: &amp;[i32]) -&gt; i32 { fn sum_of_squares(input: &amp;[i32]) -&gt; i32 {
input.iter() input.iter()
.map(|&amp;i| i * i) .map(|&amp;i| i * i)
.sum() .sum()
} }
</code></pre> </code></pre>
</section> </section>
<section> <section>
<h3>Rayon</h3> <h3>Rayon</h3>
<pre><code> <pre><code>
fn sum_of_squares(input: &amp;[i32]) -&gt; i32 { fn sum_of_squares(input: &amp;[i32]) -&gt; i32 {
input.par_iter() input.par_iter()
.map(|&amp;i| i * i) .map(|&amp;i| i * i)
.sum() .sum()
} }
</code></pre> </code></pre>
</section> </section>
<section> <section>
<h3>Log-Derive</h3> <h3>Log-Derive</h3>
@ -458,44 +510,52 @@ fn call_isan(num: &amp;str) -&gt; Result&lt;Success, Error&gt; {
} }
</code></pre> </code></pre>
</section> </section>
</section> </section>
<section> <section>
<section> <section>
<h2>E quem usa?</h2> <h2>E quem usa?</h2>
</section> </section>
<section> <section>
<h3>Magic Pocket</h3> <h3>Magic Pocket</h3>
<p>Dropbox</p> <p>Dropbox</p>
<p>Petabyte storage</p> <p>Petabyte storage</p>
</section> </section>
<section> <section>
<h3>Servo</h3> <h3>Servo</h3>
<p>Mozilla</p> <p>Mozilla</p>
<p>Base do Firefox Quantum</p> <p>Base do Firefox Quantum</p>
</section> </section>
<section> <section>
<h3>Azure</h3> <h3>Azure</h3>
<p>Microsoft</p> <p>Microsoft</p>
<p>Usado na parte de IoT do Azure</p> <p>Usado na parte de IoT do Azure</p>
</section> </section>
<section> <section>
<h3>Tor</h3> <h3>Tor</h3>
</section> </section>
</section> </section>
<section data-background='_images/thats-all-folks.jpg'> <section data-background='_images/thats-all-folks.jpg'>
</section> <div class="semi-opaque">
<ul class="empty">
<li>Júlio Biason</li>
<li>https://functional.cafe/@juliobiason</li>
<li>julio.biason@pm.me</li>
<li><a href="http://presentations.juliobiason.net">http://presentations.juliobiason.net</a></li>
</ul>
</div>
</section>
</div> </div>
</div> </div>

Loading…
Cancel
Save