Fireship · @fireship

Zig in 100 Seconds

Uploaded August 3, 2023 · Length 2:39

Zig in 100 Seconds

Summary

Zig language basics: performance, memory management, error handling, and testing in 100 seconds.

  • zig language
  • system programming
  • memory management
  • error handling
  • testing framework

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    [Music]

  2. 0:00

    Zig a high performance system

  3. 0:02

    programming language often labeled as a

  4. 0:04

    Next Generation alternative to C it was

  5. 0:06

    created by Andrew Kelly in 2016 and has

  6. 0:08

    quickly evolved into one of the most

  7. 0:10

    desired new languages in the world like

  8. 0:12

    C it's minimal extremely fast and allows

  9. 0:14

    for low-level memory control but instead

  10. 0:16

    of managing memory directly in the

  11. 0:18

    language with functions like Malik and

  12. 0:19

    free the zig standard Library ships

  13. 0:21

    allocators to provide a consistent

  14. 0:23

    interface for memory management Zig is

  15. 0:25

    not a memory safe language like rust or

  16. 0:27

    go but it doesn't have any hidden memory

  17. 0:29

    allocations making the code far more

  18. 0:31

    explicit and portable because allocators

  19. 0:33

    can be easily swapped out in the code to

  20. 0:35

    Target different architectures like x86

  21. 0:37

    arm webassembly and bare metal in

  22. 0:39

    addition Zig has no hidden control flow

  23. 0:41

    if it looks like a function and quacks

  24. 0:43

    like a function it's a function there's

  25. 0:45

    no operator overloading and it doesn't

  26. 0:47

    even have exceptions if a function can

  27. 0:48

    fail it needs to return an explicit

  28. 0:50

    error value the language also has a

  29. 0:52

    unique comp time keyword that makes it

  30. 0:54

    trivial to run Kodak compile time

  31. 0:55

    instead of runtime no preprocessor or

  32. 0:57

    macros are necessary and finally Zig can

  33. 0:59

    integrate well into a c or C plus plus

  34. 1:01

    code base and supports cross compilation

  35. 1:03

    out of the box with llvm although

  36. 1:05

    divorce paperwork has been filed to get

  37. 1:07

    started install Zig then create a new

  38. 1:09

    project with the zignet exe command in

  39. 1:12

    the main file first to import the

  40. 1:13

    standard library then Define a main

  41. 1:15

    function notice how the function returns

  42. 1:17

    a type of void with an exclamation point

  43. 1:19

    that exclamation point means that the

  44. 1:20

    function might return an error declare a

  45. 1:22

    mutable variable with the VAR keyword

  46. 1:24

    followed by a type like you wait to

  47. 1:26

    represent a single byte then assign and

  48. 1:28

    modify its value later or use cons to

  49. 1:30

    Define an immutable variable that cannot

  50. 1:32

    be changed we can also bundle multiple

  51. 1:34

    variables together into a struct then

  52. 1:36

    access them on that namespace with DOT

  53. 1:38

    notation now things start to get more

  54. 1:40

    interesting when memory management comes

  55. 1:41

    into play when initializing an array of

  56. 1:43

    integers we can allocate it to a slice

  57. 1:45

    of memory in the Heap using the built-in

  58. 1:47

    page allocator from the standard Library

  59. 1:48

    what's so cool about this is that we

  60. 1:50

    could swap it out with other allocators

  61. 1:52

    to use different memory management

  62. 1:53

    strategies now when we're done with this

  63. 1:55

    memory we need to set it free otherwise

  64. 1:57

    we could have a memory leak the defer

  65. 1:58

    keyword allows us to put that code right