Zig is general-purpose systems programming language often used as an alternative to C, C++, and Rust. Learn the basics of Zig in this quick tutorial. 

#programming #code #100secondsofcode 
 
💬 Chat with Me on Discord

https://discord.gg/fireship

🔗 Resources

Zig Lang https://ziglang.org/
C in 100 Seconds https://youtu.be/U3aXWizDbQ4
C++ in 100 Seconds https://youtu.be/MNeX4EGtR5Y

🔥 Get More Content - Upgrade to PRO

Upgrade at https://fireship.io/pro
Use code YT25 for 25% off PRO access 

🎨 My Editor Settings

- Atom One Dark 
- vscode-icons
- Fira Code Font

🔖 Topics Covered

- What is Zig?
- Is Zig fast?
- Zig quickstart tutorial
- Zig vs C
- Zig vs Rust
- Memory management in Zig explained
- How do Zig allocators work?
2024-02-04T05:01:51.109Z
nova-2
2-general-nova
2024-01-09.29447
30089e05-99d1-4376-b32e-c263170674af
bfda668e-0b61-48a2-9d92-9f730c67460f
8bbf26fec880a2b7448e61f1ebb81f786c619608f7dd839b41c57ce4e5db7c0b
67875a7f-c9c4-48a0-aa55-5bdb8a91c34a
deprecated
Zig, a high performance system programming language often labeled as a next generation alternative to C.
It was created by Andrew Kelly in 2016, and quickly evolved into one of the most desired new languages in the world.
Like C, it's minimal, extremely fast, and allows for low level memory control.
But instead of managing memory directly in the language with functions like malloc and free, the Zig standard library ships allocators to provide a consistent interface for memory management.
Zig is not a memory safe language like Rust or Go, but it doesn't have any hidden memory allocations, making the code far more explicit and portable because allocators can be easily swapped out in the code to target different architectures like x86, ARM, WebAssembly, and bare metal.
In In addition, Zig has no hidden control flow.
If it looks like a function and quacks like a function, it's a function.
There's no operator overloading, and it doesn't even have exceptions.
If a function can fail, it needs to return an explicit error value.
The language also has a unique comp time keyword that makes it trivial to run code at compile time instead of runtime.
No pre processor or macros are necessary.
And finally, Zig can integrate well into a c or c plus plus code base and supports cross compilation out of the box with LLVM, although divorce paperwork has been filed.
To get started, install Zig, then create a new project with the zig and net exe command.
In the main file, first import the standard library, then define a main function.
Notice how the function returns a type of void with an exclamation point.
That exclamation point means that the function might return an error, declare a mutable variable with the bar keyword followed by a type like q wait to represent a single byte, then assign and modify its value later, or use const to define an immutable variable that can not be changed.
We can also bundle multiple variables together into a struct, then access them on that name space with dot notation.
Now things start to get more interesting when memory management comes into When initializing an array of integers, we can allocate it to a slice of memory in the heap using the built in page allocator from the standard library.
What's so cool about this is that we could swap it out with other allocators to use different memory management strategies.
Now when we're done with this memory, we need to set it free.
Otherwise, we could have a memory leak.
And re leak.
The defer keyword allows us to put that code right next to the allocation itself and will automatically de initialize the list when it goes out of scope.
Now as we operate on the list, the try keyword provides explicit error handling.
If this line fails, it will automatically catch and return the error.
You can't just ignore it and that will make your code more reliable.
And speaking of reliability, Zig has a built in testing framework.
Use the test keyword to evaluate code outside of the main program, then use the Zig test command to run it.
And finally, build an executable with the Zig build man, and choose a build mode to optimize for speed, size, or safety.
This has been the Zig programming language in 100 seconds.
Hit the like button if you wanna see more short videos like this.
Thanks for watching, and and I will see you in the next one.

Speaker 0: Zig, a high performance system programming language often labeled as a next generation alternative to C. It was created by Andrew Kelly in 2016, and quickly evolved into one of the most desired new languages in the world. Like C, it's minimal, extremely fast, and allows for low level memory control. But instead of managing memory directly in the language with functions like malloc and free, the Zig standard library ships allocators to provide a consistent interface for memory management. Zig is not a memory safe language like Rust or Go, but it doesn't have any hidden memory allocations, making the code far more explicit and portable because allocators can be easily swapped out in the code to target different architectures like x86, ARM, WebAssembly, and bare metal.

In In addition, Zig has no hidden control flow. If it looks like a function and quacks like a function, it's a function. There's no operator overloading, and it doesn't even have exceptions. If a function can fail, it needs to return an explicit error value. The language also has a unique comp time keyword that makes it trivial to run code at compile time instead of runtime.

No pre processor or macros are necessary. And finally, Zig can integrate well into a c or c plus plus code base and supports cross compilation out of the box with LLVM, although divorce paperwork has been filed. To get started, install Zig, then create a new project with the zig and net exe command. In the main file, first import the standard library, then define a main function. Notice how the function returns a type of void with an exclamation point.

That exclamation point means that the function might return an error, declare a mutable variable with the bar keyword followed by a type like q wait to represent a single byte, then assign and modify its value later, or use const to define an immutable variable that can not be changed. We can also bundle multiple variables together into a struct, then access them on that name space with dot notation. Now things start to get more interesting when memory management comes into When initializing an array of integers, we can allocate it to a slice of memory in the heap using the built in page allocator from the standard library. What's so cool about this is that we could swap it out with other allocators to use different memory management strategies. Now when we're done with this memory, we need to set it free.

Otherwise, we could have a memory leak. And re leak. The defer keyword allows us to put that code right next to the allocation itself and will automatically de initialize the list when it goes out of scope. Now as we operate on the list, the try keyword provides explicit error handling. If this line fails, it will automatically catch and return the error.

You can't just ignore it and that will make your code more reliable. And speaking of reliability, Zig has a built in testing framework. Use the test keyword to evaluate code outside of the main program, then use the Zig test command to run it. And finally, build an executable with the Zig build man, and choose a build mode to optimize for speed, size, or safety. This has been the Zig programming language in 100 seconds.

Hit the like button if you wanna see more short videos like this. Thanks for watching, and and I will see you in the next one.
Zig, a high performance system programming language often labeled as a next generation alternative to C. It was created by Andrew Kelly in 2016, and quickly evolved into one of the most desired new languages in the world. Like C, it's minimal, extremely fast, and allows for low level memory control. But instead of managing memory directly in the language with functions like malloc and free, the Zig standard library ships allocators to provide a consistent interface for memory management. Zig is not a memory safe language like Rust or Go, but it doesn't have any hidden memory allocations, making the code far more explicit and portable because allocators can be easily swapped out in the code to target different architectures like x86, ARM, WebAssembly, and bare metal. In In addition, Zig has no hidden control flow. If it looks like a function and quacks like a function, it's a function. There's no operator overloading, and it doesn't even have exceptions.
If a function can fail, it needs to return an explicit error value. The language also has a unique comp time keyword that makes it trivial to run code at compile time instead of runtime. No pre processor or macros are necessary. And finally, Zig can integrate well into a c or c plus plus code base and supports cross compilation out of the box with LLVM, although divorce paperwork has been filed.
programming
To get started, install Zig, then create a new project with the zig and net exe command. In the main file, first import the standard library, then define a main function. Notice how the function returns a type of void with an exclamation point. That exclamation point means that the function might return an error, declare a mutable variable with the bar keyword followed by a type like q wait to represent a single byte, then assign and modify its value later, or use const to define an immutable variable that can not be changed. We can also bundle multiple variables together into a struct, then access them on that name space with dot notation. Now things start to get more interesting when memory management comes into When initializing an array of integers, we can allocate it to a slice of memory in the heap using the built in page allocator from the standard library. What's so cool about this is that we could swap it out with other allocators to use different memory management strategies. Now when we're done with this memory, we need to set it free. Otherwise, we could have a memory leak. And re leak. The defer keyword allows us to put that code right next to the allocation itself and will automatically de initialize the list when it goes out of scope. Now as we operate on the list, the try keyword provides explicit error handling. If this line fails, it will automatically catch and return the error. You can't just ignore it and that will make your code more reliable. And speaking of reliability, Zig has a built in testing framework. Use the test keyword to evaluate code outside of the main program, then use the Zig test command to run it.
And finally, build an executable with the Zig build man, and choose a build mode to optimize for speed, size, or safety. This has been the Zig programming language in 100 seconds. Hit the like button if you wanna see more short videos like this. Thanks for watching, and and I will see you in the next one.
youtube
programming
graphic design
video gaming
Zig, a high performance system programming language often labeled as a next generation alternative to C. It was created by Andrew Kelly in 2016, and quickly evolved into one of the most desired new languages in the world. Like C, it's minimal, extremely fast, and allows for low level memory control. But instead of managing memory directly in the language with functions like malloc and free, the Zig standard library ships allocators to provide a consistent interface for memory management. Zig is not a memory safe language like Rust or Go, but it doesn't have any hidden memory allocations, making the code far more explicit and portable because allocators can be easily swapped out in the code to target different architectures like x86, ARM, WebAssembly, and bare metal. In In addition, Zig has no hidden control flow. If it looks like a function and quacks like a function, it's a function. There's no operator overloading, and it doesn't even have exceptions. If a function can fail, it needs to return an explicit error value. The language also has a unique comp time keyword that makes it trivial to run code at compile time instead of runtime. No pre processor or macros are necessary. And finally, Zig can integrate well into a c or c plus plus code base and supports cross compilation out of the box with LLVM, although divorce paperwork has been filed. To get started, install Zig, then create a new project with the zig and net exe command. In the main file, first import the standard library, then define a main function. Notice how the function returns a type of void with an exclamation point. That exclamation point means that the function might return an error, declare a mutable variable with the bar keyword followed by a type like q wait to represent a single byte, then assign and modify its value later, or use const to define an immutable variable that can not be changed. We can also bundle multiple variables together into a struct, then access them on that name space with dot notation. Now things start to get more interesting when memory management comes into When initializing an array of integers, we can allocate it to a slice of memory in the heap using the built in page allocator from the standard library. What's so cool about this is that we could swap it out with other allocators to use different memory management strategies. Now when we're done with this memory, we need to set it free. Otherwise, we could have a memory leak. And re leak. The defer keyword allows us to put that code right next to the allocation itself and will automatically de initialize the list when it goes out of scope. Now as we operate on the list, the try keyword provides explicit error handling. If this line fails, it will automatically catch and return the error. You can't just ignore it and that will make your code more reliable. And speaking of reliability, Zig has a built in testing framework. Use the test keyword to evaluate code outside of the main program, then use the Zig test command to run it. And finally, build an executable with the Zig build man, and choose a build mode to optimize for speed, size, or safety. This has been the Zig programming language in 100 seconds. Hit the like button if you wanna see more short videos like this. Thanks for watching, and and I will see you in the next one.
Zig,
zig
a
a
high
high
performance
performance
system
system
programming
programming
language
language
often
often
labeled
labeled
as
as
a
a
next
next
generation
generation
alternative
alternative
to
to
C.
c
It
it
was
was
created
created
by
by
Andrew
andrew
Kelly
kelly
in
in
2016,
2016
and
and
quickly
quickly
evolved
evolved
into
into
one
one
of
of
the
the
most
most
desired
desired
new
new
languages
languages
in
in
the
the
world.
world
Like
like
C,
c
it's
it's
minimal,
minimal
extremely
extremely
fast,
fast
and
and
allows
allows
for
for
low
low
level
level
memory
memory
control.
control
But
but
instead
instead
of
of
managing
managing
memory
memory
directly
directly
in
in
the
the
language
language
with
with
functions
functions
like
like
malloc
malloc
and
and
free,
free
the
the
Zig
zig
standard
standard
library
library
ships
ships
allocators
allocators
to
to
provide
provide
a
a
consistent
consistent
interface
interface
for
for
memory
memory
management.
management
Zig
zig
is
is
not
not
a
a
memory
memory
safe
safe
language
language
like
like
Rust
rust
or
or
Go,
go
but
but
it
it
doesn't
doesn't
have
have
any
any
hidden
hidden
memory
memory
allocations,
allocations
making
making
the
the
code
code
far
far
more
more
explicit
explicit
and
and
portable
portable
because
because
allocators
allocators
can
can
be
be
easily
easily
swapped
swapped
out
out
in
in
the
the
code
code
to
to
target
target
different
different
architectures
architectures
like
like
x86,
x86
ARM,
arm
WebAssembly,
webassembly
and
and
bare
bare
metal.
metal
In
in
In
in
addition,
addition
Zig
zig
has
has
no
no
hidden
hidden
control
control
flow.
flow
If
if
it
it
looks
looks
like
like
a
a
function
function
and
and
quacks
quacks
like
like
a
a
function,
function
it's
it's
a
a
function.
function
There's
there's
no
no
operator
operator
overloading,
overloading
and
and
it
it
doesn't
doesn't
even
even
have
have
exceptions.
exceptions
If
if
a
a
function
function
can
can
fail,
fail
it
it
needs
needs
to
to
return
return
an
an
explicit
explicit
error
error
value.
value
The
the
language
language
also
also
has
has
a
a
unique
unique
comp
comp
time
time
keyword
keyword
that
that
makes
makes
it
it
trivial
trivial
to
to
run
run
code
code
at
at
compile
compile
time
time
instead
instead
of
of
runtime.
runtime
No
no
pre
pre
processor
processor
or
or
macros
macros
are
are
necessary.
necessary
And
and
finally,
finally
Zig
zig
can
can
integrate
integrate
well
well
into
into
a
a
c
c
or
or
c
c
plus
plus
plus
plus
code
code
base
base
and
and
supports
supports
cross
cross
compilation
compilation
out
out
of
of
the
the
box
box
with
with
LLVM,
llvm
although
although
divorce
divorce
paperwork
paperwork
has
has
been
been
filed.
filed
To
to
get
get
started,
started
install
install
Zig,
zig
then
then
create
create
a
a
new
new
project
project
with
with
the
the
zig
zig
and
and
net
net
exe
exe
command.
command
In
in
the
the
main
main
file,
file
first
first
import
import
the
the
standard
standard
library,
library
then
then
define
define
a
a
main
main
function.
function
Notice
notice
how
how
the
the
function
function
returns
returns
a
a
type
type
of
of
void
void
with
with
an
an
exclamation
exclamation
point.
point
That
that
exclamation
exclamation
point
point
means
means
that
that
the
the
function
function
might
might
return
return
an
an
error,
error
declare
declare
a
a
mutable
mutable
variable
variable
with
with
the
the
bar
bar
keyword
keyword
followed
followed
by
by
a
a
type
type
like
like
q
q
wait
wait
to
to
represent
represent
a
a
single
single
byte,
byte
then
then
assign
assign
and
and
modify
modify
its
its
value
value
later,
later
or
or
use
use
const
const
to
to
define
define
an
an
immutable
immutable
variable
variable
that
that
can
can
not
not
be
be
changed.
changed
We
we
can
can
also
also
bundle
bundle
multiple
multiple
variables
variables
together
together
into
into
a
a
struct,
struct
then
then
access
access
them
them
on
on
that
that
name
name
space
space
with
with
dot
dot
notation.
notation
Now
now
things
things
start
start
to
to
get
get
more
more
interesting
interesting
when
when
memory
memory
management
management
comes
comes
into
into
When
when
initializing
initializing
an
an
array
array
of
of
integers,
integers
we
we
can
can
allocate
allocate
it
it
to
to
a
a
slice
slice
of
of
memory
memory
in
in
the
the
heap
heap
using
using
the
the
built
built
in
in
page
page
allocator
allocator
from
from
the
the
standard
standard
library.
library
What's
what's
so
so
cool
cool
about
about
this
this
is
is
that
that
we
we
could
could
swap
swap
it
it
out
out
with
with
other
other
allocators
allocators
to
to
use
use
different
different
memory
memory
management
management
strategies.
strategies
Now
now
when
when
we're
we're
done
done
with
with
this
this
memory,
memory
we
we
need
need
to
to
set
set
it
it
free.
free
Otherwise,
otherwise
we
we
could
could
have
have
a
a
memory
memory
leak.
leak
And
and
re
re
leak.
leak
The
the
defer
defer
keyword
keyword
allows
allows
us
us
to
to
put
put
that
that
code
code
right
right
next
next
to
to
the
the
allocation
allocation
itself
itself
and
and
will
will
automatically
automatically
de
de
initialize
initialize
the
the
list
list
when
when
it
it
goes
goes
out
out
of
of
scope.
scope
Now
now
as
as
we
we
operate
operate
on
on
the
the
list,
list
the
the
try
try
keyword
keyword
provides
provides
explicit
explicit
error
error
handling.
handling
If
if
this
this
line
line
fails,
fails
it
it
will
will
automatically
automatically
catch
catch
and
and
return
return
the
the
error.
error
You
you
can't
can't
just
just
ignore
ignore
it
it
and
and
that
that
will
will
make
make
your
your
code
code
more
more
reliable.
reliable
And
and
speaking
speaking
of
of
reliability,
reliability
Zig
zig
has
has
a
a
built
built
in
in
testing
testing
framework.
framework
Use
use
the
the
test
test
keyword
keyword
to
to
evaluate
evaluate
code
code
outside
outside
of
of
the
the
main
main
program,
program
then
then
use
use
the
the
Zig
zig
test
test
command
command
to
to
run
run
it.
it
And
and
finally,
finally
build
build
an
an
executable
executable
with
with
the
the
Zig
zig
build
build
man,
man
and
and
choose
choose
a
a
build
build
mode
mode
to
to
optimize
optimize
for
for
speed,
speed
size,
size
or
or
safety.
safety
This
this
has
has
been
been
the
the
Zig
zig
programming
programming
language
language
in
in
100
100
seconds.
seconds
Hit
hit
the
the
like
like
button
button
if
if
you
you
wanna
wanna
see
see
more
more
short
short
videos
videos
like
like
this.
this
Thanks
thanks
for
for
watching,
watching
and
and
and
and
I
i
will
will
see
see
you
you
in
in
the
the
next
next
one.
one
success
The transcript discusses the benefits of using the new version of the language, advised against its memory safety and allowed for low level memory management. The language has a unique comp time keyword and allows for cross compilation and optimization. The program supports cross compilation and uses a build mode to optimize speed, size, or safety.
Zig in 100 Seconds
Zig, a high performance system programming language often labeled as a next generation alternative to C. It was created by Andrew Kelly in 2016, and quickly evolved into one of the most desired new languages in the world. Like C, it's minimal, extremely fast, and allows for low level memory control. But instead of managing memory directly in the language with functions like malloc and free, the Zig standard library ships allocators to provide a consistent interface for memory management. Zig is not a memory safe language like Rust or Go, but it doesn't have any hidden memory allocations, making the code far more explicit and portable because allocators can be easily swapped out in the code to target different architectures like x86, ARM, WebAssembly, and bare metal. In In addition, Zig has no hidden control flow. If it looks like a function and quacks like a function, it's a function. There's no operator overloading, and it doesn't even have exceptions. If a function can fail, it needs to return an explicit error value. The language also has a unique comp time keyword that makes it trivial to run code at compile time instead of runtime. No pre processor or macros are necessary. And finally, Zig can integrate well into a c or c plus plus code base and supports cross compilation out of the box with LLVM, although divorce paperwork has been filed. To get started, install Zig, then create a new project with the zig and net exe command. In the main file, first import the standard library, then define a main function. Notice how the function returns a type of void with an exclamation point. That exclamation point means that the function might return an error, declare a mutable variable with the bar keyword followed by a type like q wait to represent a single byte, then assign and modify its value later, or use const to define an immutable variable that can not be changed. We can also bundle multiple variables together into a struct, then access them on that name space with dot notation. Now things start to get more interesting when memory management comes into When initializing an array of integers, we can allocate it to a slice of memory in the heap using the built in page allocator from the standard library. What's so cool about this is that we could swap it out with other allocators to use different memory management strategies. Now when we're done with this memory, we need to set it free. Otherwise, we could have a memory leak. And re leak. The defer keyword allows us to put that code right next to the allocation itself and will automatically de initialize the list when it goes out of scope. Now as we operate on the list, the try keyword provides explicit error handling. If this line fails, it will automatically catch and return the error. You can't just ignore it and that will make your code more reliable. And speaking of reliability, Zig has a built in testing framework. Use the test keyword to evaluate code outside of the main program, then use the Zig test command to run it. And finally, build an executable with the Zig build man, and choose a build mode to optimize for speed, size, or safety. This has been the Zig programming language in 100 seconds. Hit the like button if you wanna see more short videos like this. Thanks for watching, and and I will see you in the next one.
The transcript discusses the benefits of using the new version of the language, advised against its memory safety and allowed for low level memory management. The language has a unique comp time keyword and allows for cross compilation and optimization. The program supports cross compilation and uses a build mode to optimize speed, size, or safety.