Fireship · @fireship
Zig in 100 Seconds
Uploaded August 3, 2023 · Length 2:39
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.
- 0:00
[Music]
- 0:00
Zig a high performance system
- 0:02
programming language often labeled as a
- 0:04
Next Generation alternative to C it was
- 0:06
created by Andrew Kelly in 2016 and has
- 0:08
quickly evolved into one of the most
- 0:10
desired new languages in the world like
- 0:12
C it's minimal extremely fast and allows
- 0:14
for low-level memory control but instead
- 0:16
of managing memory directly in the
- 0:18
language with functions like Malik and
- 0:19
free the zig standard Library ships
- 0:21
allocators to provide a consistent
- 0:23
interface for memory management Zig is
- 0:25
not a memory safe language like rust or
- 0:27
go but it doesn't have any hidden memory
- 0:29
allocations making the code far more
- 0:31
explicit and portable because allocators
- 0:33
can be easily swapped out in the code to
- 0:35
Target different architectures like x86
- 0:37
arm webassembly and bare metal in
- 0:39
addition Zig has no hidden control flow
- 0:41
if it looks like a function and quacks
- 0:43
like a function it's a function there's
- 0:45
no operator overloading and it doesn't
- 0:47
even have exceptions if a function can
- 0:48
fail it needs to return an explicit
- 0:50
error value the language also has a
- 0:52
unique comp time keyword that makes it
- 0:54
trivial to run Kodak compile time
- 0:55
instead of runtime no preprocessor or
- 0:57
macros are necessary and finally Zig can
- 0:59
integrate well into a c or C plus plus
- 1:01
code base and supports cross compilation
- 1:03
out of the box with llvm although
- 1:05
divorce paperwork has been filed to get
- 1:07
started install Zig then create a new
- 1:09
project with the zignet exe command in
- 1:12
the main file first to import the
- 1:13
standard library then Define a main
- 1:15
function notice how the function returns
- 1:17
a type of void with an exclamation point
- 1:19
that exclamation point means that the
- 1:20
function might return an error declare a
- 1:22
mutable variable with the VAR keyword
- 1:24
followed by a type like you wait to
- 1:26
represent a single byte then assign and
- 1:28
modify its value later or use cons to
- 1:30
Define an immutable variable that cannot
- 1:32
be changed we can also bundle multiple
- 1:34
variables together into a struct then
- 1:36
access them on that namespace with DOT
- 1:38
notation now things start to get more
- 1:40
interesting when memory management comes
- 1:41
into play when initializing an array of
- 1:43
integers we can allocate it to a slice
- 1:45
of memory in the Heap using the built-in
- 1:47
page allocator from the standard Library
- 1:48
what's so cool about this is that we
- 1:50
could swap it out with other allocators
- 1:52
to use different memory management
- 1:53
strategies now when we're done with this
- 1:55
memory we need to set it free otherwise
- 1:57
we could have a memory leak the defer
- 1:58
keyword allows us to put that code right


