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

Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and Cheap APIs, Zacaria Chtatar, HaveSomeCode

apidays
December 18, 2023

Apidays Paris 2023 - Forget TypeScript, Choose Rust to build Robust, Fast and Cheap APIs, Zacaria Chtatar, HaveSomeCode

Apidays Paris 2023 - Software and APIs for Smart, Sustainable and Sovereign Societies
December 6, 7 & 8, 2023

Forget TypeScript, Choose Rust to build Robust, Fast and Cheap APIs
Zacaria Chtatar, Backend Software Engineer at HaveSomeCode

------

Check out our conferences at https://www.apidays.global/

Do you want to sponsor or talk at one of our conferences?
https://apidays.typeform.com/to/ILJeAaV8

Learn more on APIscene, the global media made by the community for the community:
https://www.apiscene.io

Explore the API ecosystem with the API Landscape:
https://apilandscape.apiscene.io/

apidays

December 18, 2023
Tweet

More Decks by apidays

Other Decks in Programming

Transcript

  1. Until Nodejs V8 engine, modules system & NPM modules boom

    of easy to reuse code fullstack JS : Easy to start with, hard to master
  2. Pain points does not save you from dealing with JS

    adds types management adds static layer onto dynamic layer
  3. Pain points does not save you from dealing with JS

    adds types management adds static layer onto dynamic layer JSDoc answers to the precise problem of hinting types without getting in your way
  4. Stakes Needs worldwide scale privacy market competition environment human lives

    scalability security functionality computation time memory footprint safety
  5. Stakes Needs worldwide scale privacy market competition environment human lives

    scalability security functionality computation time memory footprint safety TypeScript is not enough
  6. enums struct FakeCat { alive: bool, hungry: bool, } let

    zombie = FakeCat { alive: false, hungry: true }; // ???
  7. enums struct FakeCat { alive: bool, hungry: bool, } let

    zombie = FakeCat { alive: false, hungry: true }; // ??? Rust makes it easy to make invalid state unrepresentable
  8. enums struct FakeCat { alive: bool, hungry: bool, } let

    zombie = FakeCat { alive: false, hungry: true }; // ??? Rust makes it easy to make invalid state unrepresentable enum RealCat { Alive { hungry: bool }, // enums can contain structs Dead, } let cat = RealCat::Alive { hungry: true }; let dead_cat = RealCat::Dead;
  9. Option enum // full code of the solution to the

    billion dollar mistake // included in the standard library enum Option<T> { None, Some(T), } let item: Option<Item> = get_item(); match item { Some(item) => map_item(item), None => handle_none_case(), // does not compile if omitted }
  10. What is wrong with this function ? function getConfig(path: string):

    string { return fs.readFileSync(path); } There is no hint that readFileSync can throw an error under some conditions
  11. Result enum enum Result<T, E> { Ok(T), Err(E), } fn

    get_config(path: &str) -> Result<String, io::Error> { fs::read_to_string(path) } // That's a shortcut, don't send me to prod ! fn dirty_get_config(path: &str) -> String { fs::read_to_string(path).unwrap() // panics in case of error }
  12. Result enum enum Result<T, E> { Ok(T), Err(E), } fn

    get_config(path: &str) -> Result<String, io::Error> { fs::read_to_string(path) } // That's a shortcut, don't send me to prod ! fn dirty_get_config(path: &str) -> String { fs::read_to_string(path).unwrap() // panics in case of error } We need to clarify what can go wrong
  13. Result enum enum Result<T, E> { Ok(T), Err(E), } fn

    get_config(path: &str) -> Result<String, io::Error> { fs::read_to_string(path) } // That's a shortcut, don't send me to prod ! fn dirty_get_config(path: &str) -> String { fs::read_to_string(path).unwrap() // panics in case of error } We need to clarify what can go wrong It's even better when it's embedded in the language
  14. Linux: The Kernel attract young devs 2/3 of vunerabilities come

    from memory management Kernel is in C and Assembly
  15. Linux: The Kernel attract young devs 2/3 of vunerabilities come

    from memory management Kernel is in C and Assembly Linus Torvalds : C++
  16. Github: 45 million repos 28 TB of unique content Code

    Search index several months with Elasticsearch 36h with Rust and Kafka 640 queries /s
  17. Cloudflare: HTTP proxy nginx not fast enough hard to customize

    in C allows to share connections between threads
  18. Cloudflare: HTTP proxy nginx not fast enough hard to customize

    in C allows to share connections between threads = 160x less connections to the origins
  19. Cloudflare: HTTP proxy nginx not fast enough hard to customize

    in C allows to share connections between threads = 160x less connections to the origins = 434 years less handshakes per day
  20. Discord: Message read service cache of a few billion entries

    every connection, message sent and read...
  21. Discord: Message read service cache of a few billion entries

    every connection, message sent and read... latences every 2 minutes because of Go Garbage Collector
  22. Discord: Message read service cache of a few billion entries

    every connection, message sent and read... latences every 2 minutes because of Go Garbage Collector
  23. Attractive Most admired language according to for 8 years !

    StackOverflow Only place where there is more devs available than offers
  24. Features static types compiled no GC compiler developed in Rust

    low and high level : zero cost abstraction no manual memory management : Ownership & Borrow checker
  25. Features static types compiled no GC compiler developed in Rust

    low and high level : zero cost abstraction no manual memory management : Ownership & Borrow checker => There is no blackbox between you and the machine
  26. Features static types compiled no GC compiler developed in Rust

    low and high level : zero cost abstraction no manual memory management : Ownership & Borrow checker => There is no blackbox between you and the machine => Better predictability
  27. Features static types compiled no GC compiler developed in Rust

    low and high level : zero cost abstraction no manual memory management : Ownership & Borrow checker => There is no blackbox between you and the machine => Better predictability => Awesome developer experience
  28. Ownership rules Only one variable owns data at a time

    Multiple readers or one writer => Memory is freed as soon as variable is out of scope
  29. Ownership rules Only one variable owns data at a time

    Multiple readers or one writer => Memory is freed as soon as variable is out of scope It's like a fundamental problem has been solved
  30. The compiler fn say(message: String) { println!("{}", message); } fn

    main() { let message = String::from("hey"); say(message); say(message); }
  31. Tools cargo test : Integration Tests, Unit Ttests cargo fmt

    cargo bench clippy : lint bacon : reload rust-analyzer : IDE developer experience
  32. Cargo doc stays up to date cargo doc --open ///

    Formats the sum of two numbers as a string. /// /// # Examples /// /// ``` /// let result = mycrate::sum_as_string(5, 10); /// assert_eq!(result, "15"); /// ``` pub fn sum_as_string(a: i32, b: i32) -> String { (a + b).to_string() } 1 2 3 4 5 6 7 8 9
  33. Cargo doc stays up to date cargo doc --open ///

    Formats the sum of two numbers as a string. /// /// # Examples /// /// ``` /// let result = mycrate::sum_as_string(5, 10); /// assert_eq!(result, "15"); /// ``` pub fn sum_as_string(a: i32, b: i32) -> String { (a + b).to_string() } 1 2 3 4 5 6 7 8 9 /// let result = mycrate::sum_as_string(5, 10); /// assert_eq!(result, "15"); /// Formats the sum of two numbers as a string. 1 /// 2 /// # Examples 3 /// 4 /// ``` 5 6 7 /// ``` 8 pub fn sum_as_string(a: i32, b: i32) -> String { (a + b).to_string() } 9
  34. Cargo doc stays up to date cargo doc --open ///

    Formats the sum of two numbers as a string. /// /// # Examples /// /// ``` /// let result = mycrate::sum_as_string(5, 10); /// assert_eq!(result, "15"); /// ``` pub fn sum_as_string(a: i32, b: i32) -> String { (a + b).to_string() } 1 2 3 4 5 6 7 8 9 /// let result = mycrate::sum_as_string(5, 10); /// assert_eq!(result, "15"); /// Formats the sum of two numbers as a string. 1 /// 2 /// # Examples 3 /// 4 /// ``` 5 6 7 /// ``` 8 pub fn sum_as_string(a: i32, b: i32) -> String { (a + b).to_string() } 9 /// Formats the sum of two numbers as a string. /// /// # Examples /// /// ``` /// let result = mycrate::sum_as_string(5, 10); /// assert_eq!(result, "15"); /// ``` pub fn sum_as_string(a: i32, b: i32) -> String { (a + b).to_string() } 1 2 3 4 5 6 7 8 9
  35. Possible struggles projects move slower embrace the paradigm takes 3

    to 6 months to become productive build time work with external libraries ecosystem calmer than JS
  36. In other languages simple things are easy and complex things

    are possible, in Rust simple things are possible and complex things are EASY.
  37. Get started as dev - reference - condensed reference -

    exercises - quick walkthrough - complete walkthrough - quick videos - longer videos - Youtube & paid bootcamp - not sponsored - keywords discovery - keywords discovery Rust book Rust by example Rustlings A half-hour to learn Rust Comprehensive Rust by Google Noboilerplate Code to the moon Let's get rusty Awesome Rust Roadmap
  38. Get started as manager find dev interested in Rust: there

    are a lot start with simple projects: CLI lambdas microservice network app devops tools
  39. Get started as manager find dev interested in Rust: there

    are a lot start with simple projects: CLI lambdas microservice network app devops tools Make the world a safer, faster and sustainable place
  40. Q&A

  41. Governance Release every 6 weeks Backward compatibility Breaking changes are

    opt-in thanks to Rust Project Rust Foundation editions