HPR3426: Rust 101: Episode 0 - What in Tarnishing?

 
Поделиться
 

Manage episode 302658493 series 108988
Сделано HPR Volunteer and Hacker Public Radio и найдено благодаря Player FM и нашему сообществу. Авторские права принадлежат издателю, а не Player FM, и аудиоматериалы транслируются прямо с его сервера. Нажмите на кнопку Подписаться, чтобы следить за обновлениями через Player FM или скопируйте и вставьте ссылку на канал в другое приложение для подкастов.

Talking Points

  • What is Rust?
    • " Garbage Collection " - Resource Acquisition Is Initialization (RAII)
    • Strict Typing with Type Inference
    • Reference pointers
    • Immutable by default
    • Unsafe Mode
  • Why use Rust over Python?
    • Speed
    • Compiled
      1. Help from compiler
      2. Smaller binary size
      3. Useful in high throughput/embedded applications
    • Logically consistent
  • Why use Rust over C?
    • Safe by default
    • Easier to read
    • Forces you to write good code
    • Arrays without stupidity++ and built in vectors
    • Option and Result or a match {} made in heaven

Show Notes

Strict Typing

fn main() { // Type declared with var: syntax let penguin_one: &str = "gentoo"; // Type &str is inherited from "gentoo" let penguin_two = "gentoo"; // Will not panic if they are the same assert_eq!(penguin_one, penguin_two); }

Reference Pointers

Wrong Way:

fn print_u8_vector(vec: Vec) { println!("{:?}", vec); } fn main() { let penguin_ages: Vec = vec!(2, 4, 6); print_u8_vector(penguin_ages); // This line will throw an error println!("{}", penguin_ages[0]); }

Correct Way:

fn print_u8_vector(vec: &Vec) { println!("{:?}", vec); } fn main() { let penguin_ages: Vec = vec!(2, 4, 6); print_u8_vector(&penguin_ages); // This line will print '2' println!("{}", penguin_ages[0]); }

Immutable By Default

Wrong Way:

fn main() { let my_num = 2; // This line will throw an error my_num = my_num + 1; println!("{}", my_num); }

Correct Way:

fn main() { let mut my_num = 2; my_num = my_num + 1; // This line will print '3' println!("{}", my_num); }

Unsafe Code

Hello World Program in C in Rust:

extern "C" { fn printf(input: &str); } fn main() { unsafe { printf("Hello, World!"); } }

Contact Me

Email: izzyleibowitz at pm dot me

Mastodon: at blackernel at nixnet dot social

3461 эпизодов