Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Две-три думи за Rust (ТърновоКонф 2018)

Две-три думи за Rust (ТърновоКонф 2018)

Демонстрация на това защо Rust е интересен език, който си заслужава да се пробва.

Andrew Radev

June 02, 2018
Tweet

More Decks by Andrew Radev

Other Decks in Programming

Transcript

  1. Защо? • Скорост • Съвместимост • C, Ruby, Python, Erlang

    • Микроконтролери • Web Assembly • Безопасност
  2. Защо? • Скорост • Съвместимост • Безопасност • Елегантност •

    Общност: документация, конференции, форуми, процес на разработка
  3. Zero-cost abstraction C++ implementations obey the zero-overhead principle: What you

    don’t use, you don’t pay for [Stroustrup, 1994]. And further: What you do use, you couldn’t hand code any better. – Stroustrup
  4. fn main() { let potato = " Любов, любов, варен

    картоф, разрежеш го, а той суров. "; println!("--\n{}\n--", potato); } // =================== --- Любов, любов, варен картоф, разрежеш го, а той суров. ---
  5. fn main() { let potato = " Любов, любов, варен

    картоф, разрежеш го, а той суров. "; let lines = potato. trim(). lines(). map(|l| l.trim()); println!("---"); for line in lines { println!("{}", line); } println!("---"); } // =================== --- Любов, любов, варен картоф, разрежеш го, а той суров. ---
  6. fn main() { let potato = " Любов, любов, варен

    картоф, разрежеш го, а той суров. "; let lines = potato. trim(). lines(). map(|l| l.trim()); println!("+-------------------------------+"); for line in lines { println!("| {}\t|", line); } println!("+-------------------------------+"); } // =================== +-------------------------------+ | Любов, любов, варен картоф, | | разрежеш го, а той суров. | +-------------------------------+
  7. let lines = potato. // => &str trim(). lines(). map(|l|

    l.trim()); for line in lines { println!("{}", line); } fn trim(&self) -> &str { // // " Любов, ала-бала, картоф " // ^___________________________^ -> текущия &str // // " Любов, ала-бала, картоф " // ^_____________________^ -> върнатия &str }
  8. let lines = potato. trim(). lines(). // => Lines (std::str::Lines)

    map(|l| l.trim()); for line in lines { println!("{}", line); }
  9. let lines = potato. trim(). lines(). map(|l| l.trim()); // =>

    std::iter::Map<std::str::Lines<'_>, [closure@src/main.rs:10:13: 10:25]> for line in lines { println!("{}", line); }
  10. file, err := os.Open("filename.ext") if err != nil { return

    err } match File::open("filename.ext") { Ok(file) => file, Err(e) => return e, } enum Result<T, E> { Ok(T), Err(E), } Result<File, Error>
  11. use std::fs::File; use std::io::{self, Read}; fn main() { match do_magic_stuff()

    { Ok(contents) => println!("{}", contents), Err(e) => panic!("OH, NO! {}", e), } } fn do_magic_stuff() -> Result<String, io::Error> { let mut boring = match File::open("boring.txt") { Ok(f) => f, Err(e) => return Err(e), }; let mut fun = match File::open("fun.txt") { Ok(f) => f, Err(e) => return Err(e), }; let mut contents = String::new(); boring.read_to_string(&mut contents); fun.read_to_string(&mut contents); return Ok(contents); }
  12. use std::fs::File; use std::io::{self, Read}; fn main() { match do_magic_stuff()

    { Ok(contents) => println!("{}", contents), Err(e) => panic!("OH, NO! {}", e), } } fn do_magic_stuff() -> Result<String, io::Error> { let mut boring = match File::open("boring.txt") { Ok(f) => f, Err(e) => return Err(e), }; let mut fun = match File::open("fun.txt") { Ok(f) => f, Err(e) => return Err(e), }; let mut contents = String::new(); boring.read_to_string(&mut contents); fun.read_to_string(&mut contents); return Ok(contents); }
  13. macro_rules! try { ($expr:expr) => { match $expr { Ok(result)

    => result, Err(e) => return Err(e), } } }
  14. use std::fs::File; use std::io::{self, Read}; fn main() { match do_magic_stuff()

    { Ok(contents) => println!("{}", contents), Err(e) => panic!("OH, NO! {}", e), } } fn do_magic_stuff() -> Result<String, io::Error> { let mut boring = match File::open("boring.txt") { Ok(f) => f, Err(e) => return Err(e), }; let mut fun = match File::open("fun.txt") { Ok(f) => f, Err(e) => return Err(e), }; let mut contents = String::new(); boring.read_to_string(&mut contents); fun.read_to_string(&mut contents); return Ok(contents); }
  15. use std::fs::File; use std::io::{self, Read}; fn main() { match do_magic_stuff()

    { Ok(contents) => println!("{}", contents), Err(e) => panic!("OH, NO! {}", e), } } fn do_magic_stuff() -> Result<String, io::Error> { let mut boring = try!(File::open("boring.txt")); let mut fun = try!(File::open("fun.txt")); let mut contents = String::new(); boring.read_to_string(&mut contents); fun.read_to_string(&mut contents); return Ok(contents); }
  16. use std::fs::File; use std::io::{self, Read}; fn main() { match do_magic_stuff()

    { Ok(contents) => println!("{}", contents), Err(e) => panic!("OH, NO! {}", e), } } fn do_magic_stuff() -> Result<String, io::Error> { let mut boring = File::open("boring.txt")?; let mut fun = File::open("fun.txt")?; let mut contents = String::new(); boring.read_to_string(&mut contents); fun.read_to_string(&mut contents); return Ok(contents); }
  17. Досадна безопасност • Няма NULL • 3.14 * 2 е

    компилационна грешка • Жонглиране на references (“псевдоними”) • (demo)
  18. Полезна безопасност :) • HashMap.entry() • Има Result, Option –

    по-удобни от NULL • Добре документирани и полезни операции с числа • (demo)
  19. Learning curve • “Това е ужасно, защо не мога да

    направя каквото си знам”? • Крачка назад • Четене на документация • Различен (по-добър) подход
  20. ?