class: center, less-space, title-bg, title background-image: url(media/metal-ferris.jpg) background-size: contain # Mundo Oxidado ## A Rusty World ### 2017-10-04 - Nardoz Buenos Aires ### Jan-Erik Rediger — [@badboy_](https://twitter.com/badboy_) ??? Buenos Noches Thanks for coming, I’m here because Seppo knew I was visiting BA and asked me to give a talk. I will do this mostly in English because my Spanish is limited to one full but important sentence: Donde esta mi cerveza? As a German that’s enough to survive *click* So who am I? --- ## It's me, Jan-Erik
-- ## Traveler -- ## Software Developer
& Student
-- ## Mozilla TechSpeaker -- ## Rustacean --
--- class: rfz background-image: url(media/rustfest-crowd.jpg) background-size: contain ![Rustfest Zurich](media/RFZ-logo.png) ??? I was also a co-organizer of the first two RustFests, the european Rust conference. The third iteration happened last weekend in Zurich, Switzerland. --- class: imgnormal, center, middle background-image: url(media/metal-ferris.jpg) background-size: contain ??? Before we get into Rust, I want to know a bit about you Raise your hands. --- class: center, middle, imglogo # Developer? ??? Who here writes code for a living? --- class: center, middle, imglogo # Not a developer? ??? who here does not write code directly, e.g. documentation, project management or something completely different? --- class: center, middle, imglogo # Your language of choice? ??? Now on to languages. Backend devs? Frontend devs? Fullstack? Java? C? Python/Ruby? anything I missed? --- class: center, middle, imglogo # Rust Dev? ??? Who does Rust? --- class: center, middle, imgnormal ![](media/rust-logo.png) ??? Good. So I know there were already talks about rust here. --- background-image: url(media/better-dev-tweet.png) .footnote[["Rust made me a better C programmer."](https://twitter.com/badboy_/status/820054791397244932)] ??? I was even quoted! --- class: ## Rust ```rust fn main() { let program = "+ + * - /"; let mut accumulator = 0; for token in program.chars() { match token { '+' => accumulator += 1, '-' => accumulator -= 1, '*' => accumulator *= 2, '/' => accumulator /= 2, _ => { /* ignore everything else */ } } } println!("The program \"{}\" calculates the value {}", program, accumulator); } ``` .footnote[["Rust for Rubyists"](https://fnordig.de/talks/2014/froscon/rust-for-rubyists/#/), FrOSCon, RedFrogConf, 24.08.2014] ??? This is Rust. It's actually the example I used in my very first rust talk more than 3 years ago It was also the one used on the website --- class: center, middle, bold > Rust is a systems programming language that
runs blazingly fast
,
prevents segfaults
, and guarantees
thread safety
. ??? Rust itself advertises it by this sentence: **Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety.** --- class: center, middle, highl > Rust is a systems programming language that
runs blazingly fast
,
prevents segfaults
, and guarantees
thread safety
. ??? A bit much. Let's focus on the core aspects. blazingly fast prevents segfaults thread safety I will tell you how it achieves all of that --- class: ## Historia * started in 2010 by Graydon Hoare * Version 1.0 in May 2015 * Current: 1.20\* * sponsored by Mozilla .footnote[\* v1.21 on October 12th] ??? Historia. I'm getting good at this Spanish thing, right? --- class: ## Why Rust? -- * Memory safety -- * "Zero-Cost abstractions" -- * FFI for interoperability with C -- * low-level where necessary --- class: center, middle > Rust exists to write tools that already exist elsewhere. >
Lucas (
@moonbeamlabs
)
.footnote[["Nerdkunde 43" (German Podcast)](http://www.nerdkunde.de/45-die-menschliche-moehre)] ??? Another friend put it this way --- class: ## Where Rust? --- class: gif background-image: url(media/rust-friends.gif) --- class: center, imgnormal background-image: url(media/einstein-on-the-beach.jpg) background-size: contain ![](media/tdo_schauspiel.png) ![](media/rrbone.png) .footnote[[midioscar](https://github.com/rrbone/midioscar), [Einstein on the Beach](https://www.theaterdo.de/detail/event/einstein-on-the-beach/)] ??? a midi to osc proxy --- ## How Rust? -- * Open development on [github.com/rust-lang](https://github.com/rust-lang/) -- * RFCs for all changes -- * Frequent, constant releases (6 weeks) --- ## Core concepts * Ownership & Borrowing * Explicitness\* * Compiler support * Better tooling .footnote[\* unless too much] --- class: ## Ownership & Borrowing .left-column[ ### Ownership * Data has a single owner * Responsible for resources ] -- .right-column[ ### Borrowing ### Either * multiple immutable borrows ### or * one mutable borrow ] --- class: center, middle # Ready for some code? --- class: ## Ownership & Borrowing .left-column[ ### Ownership ```rust fn greet(name: String) { println!("¡Hola, {}!", name); } ``` ] -- .right-column[ ### Immutable borrowing ```rust fn greet(name: &String) { println!("¡Hola, {}!", name); } ``` ### Mutable borrowing ```rust fn greet(name: &mut String) { name.push_str(", hola!"); } ``` ] .footnote[[Playground Example](https://play.rust-lang.org/?gist=a25652bae5823534267b878e50ac2c6d&version=stable)] --- ## Explicitness: Error Handling .left-column30[ ### (Generic) Result ```rust enum Result<T, E> { Ok(T), Err(E) } ``` ] -- .right-column60[ ### Error handling ```rust fn this_can_fail(succeeds: bool) -> Result<String, String> { if succeeds { Ok(String::from("Success")) } else { Err(String::from("Error")) } } fn main() { let outcome = this_can_fail(true); println!("{:?}", outcome); } ``` ] .footnote[[Playground Example](https://play.rust-lang.org/?gist=783ffd3b75f34e3706c2dc75e6e35639&version=stable)] --- ## Compiler Support ```rust fn main() { let meetup = "Nardoz"; println!("{}", metup); } ``` -- ```rust $ rustc hello.rs error[E0425]: cannot find value `metup` in this scope --> hello.rs:3:20 | 3 | println!("{}", metup); | ^^^^^ did you mean `meetup`? error: aborting due to previous error ``` .footnote[[Playground Example](https://play.rust-lang.org/?gist=59afbf3225e84dde01722654418d893a&version=stable)] --- ## Compiler Support ```rust fn borrow_same_field_twice_mut_mut() { let mut foo = make_foo(); let bar1 = &mut foo.bar1; let _bar2 = &mut foo.bar1; *bar1; } ``` -- ```rust $ rustc hello.rs error[E0499]: cannot borrow `foo.bar1` as mutable more than once at a time --> hello.rs:10:22 | 9 | let bar1 = &mut foo.bar1; | -------- first mutable borrow occurs here 10 | let _bar2 = &mut foo.bar1; | ^^^^^^^^ second mutable borrow occurs here 11 | *bar1; 12 | } | - first borrow ends here error: aborting due to previous error ``` .footnote[[Playground Example](https://play.rust-lang.org/?gist=581129bff87d864f17a20cbde0d41809&version=stable)] --- class: ## Tooling: Cargo * package manager, build system, registry -- *
7600
11600 Crates -- * `cargo new` → project setup -- * `cargo build` → download deps & build .footnote[[crates.io](https://crates.io)] --- class: ## More tooling -- * RLS → IDE-like support everywhere -- * `rustfmt` → one format to rule them all -- * `clippy` → a bunch of lints --- class: center, middle # Demo Time\* .footnote[\* requires Rust Nightly…] --- ## #rust IRC channel search index * index 3 years of IRC chatter * web frontend for users * search by nick and messages .footnote[(for lack of own projects to show I built on top of others!)] --- class: center, middle, imgnormal ![](media/search-frontend.png) --- ## Demo Time - Stats * one night of hacking * ~350 lines of code * 162M input data (july 2014 - october 2017) * 185M index (with all data) * 16s indexing time * blazingly fast web frontend --- ## Demo Time - Resources * [Tantivy](https://github.com/tantivy-search/tantivy]): full-text search engine * [Rocket](https://rocket.rs/): web framework * [regex](https://github.com/rust-lang/regex) : one of the fastest regex engines * [Serde](https://serde.rs/):
a
**the** framework for serializing and deserializing --- class: ## más óxido * [rust-lang.org](https://www.rust-lang.org/) * [community.rs/resources](https://community.rs/) * [play.rust-lang.org](https://play.rust-lang.org/) --- class: center, middle, title # ¡Muchas gracias! ## [@badboy_](https://twitter.com/badboy_) / janerik@fnordig.de