►
Description
Everybody knows constants from other programming languages. But as so often, Rust is different and knows a lot of tricks regarding constants that other languages don't. Rust's *const fn* recently got new features (v1.61), so we decided to do an overview session about consts in Rust. This is a beginner-friendly session for people who are starting with Rust or haven't taken a deeper look into constants and *const fn* in Rust.
A
I'm
going
to
share
my
screen
now
and
I
hope
you
can
see
it
well.
Is
this
the
correct
screen?
Sorry,
if
I
need
a
second,
and
here
it
is
yes,
it
works
perfectly
fine.
So
it's
a
little
bit
hard.
Please
excuse
me
jiggling
around
with
my
slides
here
and
my
my
environment,
because
doing
the
moderation
and
doing
the
talk
at
the
same
time.
Isn't
that
simple
and
I
hope
it
will
turn
out
nicely.
So,
let's
dive
right
into
it.
A
The
next
talk
the
next
I
don't
know
half
an
hour
or,
however
long
it
will
take
us.
If
you
have
any
questions,
please
send
them
in
the
chat.
I
will
try
to
monitor
the
chat
window
here.
The
next
talk
will
be
I.
Call
this
constant
fun
with
rust.
You
know
my
personal
background
is
very
much
in
in
managed
languages
like
C,
sharp
and
I
did
a
lot
of
typescript
development.
A
I
did
a
lot
of
go
development
and,
in
the
last
few
years,
I
focused
a
lot
on
Rust
and
one
of
the
things
that
struck
me
when
I
used
when
I
started
to
work
with
with
rust
was
the
possibilities
that
you
have
when
working
with
consts
when
working
with
constants,
and
especially
when
working
with
constant
evaluation
in
in
constant
functions,
and
this
is
exactly
what
I
would
like
to
do
today,
together
with
you.
A
This
is
a
beginner
friendly
talk,
so
if
you
are
rather
new
to
rust
or
if
you
have
already
a
little
bit
of
rust
knowledge-
and
you
would
like
to
deepen
your
knowledge
in
this
specific
topic-
that's
exactly
correct.
That's
exactly
the
right
talk
for
you!
So,
let's
get
started,
I
have
no
slides.
I
will
do
some
live
coding,
but
I
have
prepared
a
lot
of
samples.
So
you
will
see
me
copying
some
code
in,
but
we
will
also
do
some
live
coding
here.
So
let's
get
started
and
get
right
into
to
the
basics.
A
First,
it's
very
simple.
So
if
we
have
I,
don't
know
something
like
this
nearly
the
answer.
We
can
Define
consts.
As
you
obviously
know
them
from
other
programming
languages,
that's
very,
very
obvious,
and
then
we
can
I,
don't
know,
use
these
consts
and
use
them
in
order
to
build
additional
cons.
Here
you
see
them
answer
nearly
the
answer,
plus
plus
one,
and
then
we
can
take
this
one.
Let
me
copy
in
some
code
and
print
it
on
the
screen.
I
mean
that
is
obvious.
I
don't
have
to
talk
a
lot
about
it.
A
A
Rust
is
not
just
capable
of
doing
numeric
cons.
Of
course,
what
you
can
also
do
is,
you
can
add,
for
instance,
a
constant
string,
and
that
would
look
like
that.
Let
me
put
in
that
here.
That
is
just
a
constant
string.
Nothing
special
works
like
a
charm.
We
also
can
use
constant
arrays.
Something
like
this.
Let
me
put
that
one
here.
We
can
do
that,
absolutely
no
problem,
Works
oops!
Sorry,
it
should
be
here
works
like
charm,
so
this
is
what
you
might
expect
when
you
come
from
a
different
programming
language.
A
This
is
what
you
already
know.
Let
me
surprise
you
with
the
first
slight
small
things.
Const
in
Rust
do
not
need
to
be
declared
on
the
top
level.
You
can
declare
constants
in
Rust,
wherever
you
want,
wherever
you
want,
you
can,
for
instance,
Define.
Let
me
show
you
that
here
you
can
define
a
constant
in
the
middle
of
the
main
method.
This
constant
now
has
a
scope,
it's
scoped
to
the
main
method,
and
we
can
only
use
it
within
this
method.
Keep
that
in
mind,
because
in
a
few
minutes
we
will
need
that.
A
We
will
need
that
because
we
will
find
out
that
scoped
constants
have
very
specific
properties
and
capabilities
and
I
will
show
you
how
this
works.
So
this
was
the
basic
stuff
check.
First
lesson
done:
this
is
the
usual
con
stuff
with
a
little
bit
extra.
Now,
let's
take
a
look
at
more
interesting
samples.
First,
one
is
I
would
like
to
show
you
that
rust
constants
don't
need
to
be
basic
data
types.
We
can
in
fact
use
more
complicated
data
types.
Let
me
copy
in
some
code
here.
This
is
a
customer
object.
A
This
customer
object
has
a
string
in
it
and
an
age
in
it,
and
now
I
would
like
to
define
a
constant
customer
for
whatever
reason
I
might
want
that,
but
I'm
definitely
capable
of
doing
that.
I
can
Define
here.
A
constant
customer
and
I
can
use
the
struct
that
I
used
before
and
this
definitely
works.
Let
me
show
you
that
I
can
add
a
print
statement
here
in
order
to
get
rid
of
the
warnings,
and
now
we
have
a
constant
customer,
so
structured
data
types
can
definitely
work
as
as
constants
here
right
that
works
nice.
A
A
It's
rocked
I
called
here
named
numbers
and
when
I
declare
the
constant
struct
named
numbers
here
can
I
refer
to
the
constants
up
here.
Yes,
I
definitely
can
so.
If
you
have
constants
in
this
case,
I
have
a
string
and
I
have
an
array.
You
can
always
refer
to
these
constants
and
get
the
the
address
get
the
reference
to
these
constants.
So
with
this,
we
create
a
named
numbers,
constant,
which
is
a
structured
data
type
where
I
use
a
constant
text
as
the
name
and
a
constant
number
array
here
at
this
member.
A
This
works
perfectly
fine
and
I
can
prove
that
to
you
by
copying
here
into
the
main
method.
You
can
try
all
these
things
yourself
after
this
talk,
I
can
copy
it
in
and
it
works
perfectly
fine
I
can
enter
over
the
name
numbers.
It
will
get
me
the
correct
sum.
Trust
me:
I
will
not
now
not
run
the
program,
because
you
will
probably
guess
what
the
result
will
be.
So
this
is
kind
of
kind
of
surprising
already,
but
let
me
surprise
you
even
more
for
that
I'm
going
to
clean
up
a
little
bit
here.
A
A
Yes,
a
trait
is
something
like
an
interface
very
similar
to
what
you
know
from
an
interface
from
other
programming
languages,
but
in
this
case
we
can
have
a
required
constant,
an
Associated,
constant,
a
constant
associated
with
the
type
implementing
the
trade
we
can
have
that
requirement
inside
of
the
trait
the
second
one
is
very
similar.
It
is
also
a
constant,
a
requirement
on
the
trait,
but
we
can
provide
a
default
value
already
in
the
trait
now,
let's
create
a
structure.
I
have
numbers
here
we
are
and
implement.
A
A
We
can
even
override
the
default
value.
I've
prepared,
another
struct
with
another
interface
implemented
with
another
struct,
a
trait
with
another
struct
here
for
you
here,
you
see,
I
have
the
type
I
have
other
numbers
and
in
this
case,
I'm
implementing
the
existing
has
numbers
straight
by
overriding
the
default
value,
which
came
before
directly
from
this
trait
here
here.
A
I'm
overriding
the
default
value
for
the
constant
inside
the
associated
type
and
I
can
immediately
use
it,
as
you
can
see
here,
and
that
will
give
me
an
array
containing
one
two,
three,
four
six
because
I,
oh
I've,
overwritten
the
default
value
of
the
constant
from
the
trait.
So
keep
that
in
mind
traits
support
costs.
A
The
concepts
that
you
saw
before
are
free
constants.
They
are
not
associated
to
a
type,
but
here
we
have
cons
Associated
to
a
type.
In
this
case,
the
type
I
have
other
numbers
and
those
costs
can
be
required
by
a
trait.
So
maybe
you
have
learned
something
new
about
rust
that
you
don't
know
from
other
programming
languages.
Next
chapter
check:
let's
go
into
the
next
topic.
A
Another
thing
that
is
possible
with
cons
that
you
might
not
expect
from
when
you
come
from
other
programming
languages.
Let's
define
a
very
simple
structure
here.
This
one
will
say
goodbye
and
you
maybe
can
already
guess
what
we'll
say.
Goodbye
does
we'll
say
goodbye,
as
you
can
see
here
is
a
struct
which
consists
just
of
a
string.
It
contains
a
string,
and
that
will
be
the
goodbye
message
now.
Let's
implement
the
drop
trait
for
we'll
say
goodbye.
A
The
drop
tricks
does
nothing
more
than
just
printing
out
the
goodbye
message
that
we
passed
into
the
will
say
goodbye
structure
here
now
we
can
do
something
interesting.
We
can
say:
hey,
let's
take
this
constant
here
this
one
and
we
find
it
here
constant
goodbye
in
German,
it
is
of
type
will
say
goodbye
and
we
say
we'll
say
goodbye,
Alf
wiedersine,
which
is
the
German
version
of
goodbye,
and
now
we
can
use
this
constant
inside
of
our
main
method
in
this
case
I'm
putting
it
inside
of
a
scope.
So
what
will
happen
here?
A
We
will
copy,
because
consts
are
by
default
copied
and
we
will
see
that
later
on.
We
will
copy
the
will
say
goodbye,
constant
here
and
this
constant.
The
type
of
the
constant
has,
in
this
case
a
kind
of
Destructor,
and
it
will
print
alph
wiedersine
on
standard
out,
because
the
variable
goes
out
of
scope
exactly
here.
So,
as
you
can
see,
constants
can
have
these
structures
and
if
we
use
it
like
that,
they
will
be
executed.
Okay,
so
interesting
thing.
A
What
you
see
is
that
we
are
reaching
a
point
where
we
will
see
that
consts
in
Rust
are
not
just
constant
values,
but
they
they
essentially
contain
code
that
is
executed
at
run
at
compile
time,
and
the
next
example
that
I'm
going
to
show
you
goes
into
exactly
that
direction.
Let
me
get
rid
of
that.
One,
as
I
said,
I
will
share
the
GitHub
link
of
the
whole
example
at
the
end
of
the
talk.
So
don't
worry,
it
will
work
just
just
fine.
A
Let
me
see
yeah
here
we
are,
let's
add
some
code.
This
has
nothing
to
do
with
consts.
It
is
just
the
thing
that
we
had
before
has
numbers
yeah.
We
saw
that
code
before
I
have
numbers
yeah.
We
saw
that
code
before
now.
Imagine
we
would
like
to
write
code.
The
kind
of
test
that
verifies
that
I
have
numbers
implements
the
has
numbers
straight.
How
could
we
do
that?
Well,
I
have
a
bunch
of
interesting
things
for
you
here.
First
we
can
Define
constants,
which
don't
have
a
name.
They
are
Anonymous.
A
We
can
use
this
discard
operator
the
underscore
to
tell
rust
that
we
don't
need
a
name
for
this
constant.
You
might
wonder
now.
Why
should
we
create
the
constant
without
the
name
if
the
constant
doesn't
have
a
name?
We
can
never
ever
refer
to
this
constant
because
it
doesn't
have
a
name.
That's
the
whole
point
of
it.
It
will
get
even
stranger
before
it
makes
sense.
Let's
define
the
type
of
this
Anonymous
constant
and,
in
this
case,
I
make
it
a
unit
I
make
it
a
unit.
A
So
with
this,
it
can't
just
have
a
single
value.
The
unit
that
doesn't
make
any
sense
right,
we
have
an
anonymous
constant,
which
just
can
contain
a
single
value
the
unit.
So,
let's
assign,
let's
assign
here
a
value
for
this
constant,
and
we
can
do
it
exactly
something
like
this
okay,
so
this
will
now
be
code.
This
is
a
code
block
that
runs
and
returns
a
unit
to
assign
it
to
this
unit
to
a
constant
which
doesn't
have
a
name.
What
is
the
whole
point
of
that?
A
So
what
we
can
do
is
we
can
add
any
code
here,
not
really
any
code.
There
are
some
limitations,
so
you
can't
write
any
code
at
that
point.
I
will
show
you
some
limitations
later
on,
but
we
can,
in
general,
write
nearly
any
code
and
execute
it
at
compile
time.
So
here
I
have
prepared
or
some
sample
code
here,
and
this
code
will
now
just
use.
As
you
can
see
here.
A
This
struct
implements
my
trait,
which
uses
the
has
number
trait
here
and
then
we
will
take
the
I
have
numbers,
and
with
this
code
using
the
Phantom
data
and
this
helper
struct,
we
can
verify
that
I
have
number
must
implement.
The
trait
has
numbers.
The
whole
point
that
we
have
here
if
I
remove
that
we
will
get
an
error,
obviously,
because
it
doesn't
satisfy
the
requirements
of
this.
This
trade
yeah
it
really.
A
The
whole
point
is
that
this
code
is
now
running
at
compile
time
not
running
at
runtime,
and
this
is
what's
possible
with
constant
function:
evaluation
here
in
Rust
granted,
you
will
probably
not
write
this
code
very
often
code
like
that,
but
we,
it
was
just
an
introduction
for
what
we
can
really
do
with
this
constant
evolution
of
functions
at
compile
time.
So,
let's
dive
right
into
the
next
example,
which
goes
into
exactly
the
same
direction.
A
Let's
copy
here
this
sample
code
here
this
is
a
regular
function.
This
is
a
function,
let's
remove
the
const
here.
This
is
a
regular
function,
generating
five
numbers.
It
does
it
rather
strangely,
because
we
don't
use
a
for
Loop
here
we
use
a
helper
variable,
a
while
loop
and
just
fill
an
array
of
five
numbers
with
some
values
and
return
this
array.
A
The
point
why
I
do
the
for
Loop
here
is
that
I
can
put
the
keyword
const
in
front
of
this
function,
five
numbers
and
by
putting
the
const
keyword
here,
we
allow
rust
to
run
this
function
at
compile
time
when
generating
the
the
the
value
for
a
constant
and
very
important.
We
can
also
use
it
to
initialize
static
members
static
variables.
So,
if
you
have
static
variables,
we
can
also
initialize
the
static
variable
with
the
result
of
a
constant
function.
A
So
what
we
can
now
do
is
we
can
define
a
constant
with
five
numbers,
let's
put
a
bit
this
year
in
the
main
method,
and
we
assign
the
result
of
five
numbers
here
to
exactly
that.
Constant.
This
code
does
not
exist
at
runtime,
it
is
executed
at
compile
time
and
we
have
this
contact.
This
constant
and
the
value
inside
of
this
constant
is,
is
compiled
into
our
executable
and
just
read
from
the
executable.
A
That's
it
so
it's
really
a
constant
value
and
the
reason
why
I
didn't
use
a
for
Loop
here
is
because
for
Loop,
unfortunately,
are
not
supported
inside
of
of
constant
evaluated
functions.
It's
not
possible,
don't
get
me
wrong.
We
don't
need
to
use
a
constant
here.
We
can
also
use
a
variable,
so
constant
functions
are
not
limited
to
applying
them
at
compile
time.
We
can
also
apply
them
at
runtime.
In
this
case,
I
have
a
let
not
a
const,
so
I'm
using
the
five
numbers
function
at
runtime
and
now
rust
will
compile
this
function.
A
Put
it
into
my
executable
and
I
can
use
it.
I
can
use
it
at
runtime.
So
this
is
what
we
see
here.
We
can
go
one
step
further.
Let
me
show
you
what
we
can
do
if
we
add
a
little
bit
more
here
now.
This
method
numbers
is
a
generalization
of
five
numbers.
What
it
does
exactly
the
same
as
before,
but
the
big
difference
is
that
the
upper
limit
of
the
while
loop
here
is
passed
in
using
a
const
generic.
A
So
the
takeaway
here
is
that
const
functions
are
very
powerful,
they
can
be
generic
and
they
can
even
receive
const
generic
attributes,
and
that
gives
us
a
lot
of
power,
because
with
that
we
can
generate
an
arbitrary
number
of
numbers.
In
our
case,
we
can
declare
10
numbers
as
an
array
with
size.
10.
now
rust
will
call
this
method
here.
It
will
derive
the
constant
generic
argument
directly
from
the
type
Declaration
on
the
left
hand
side.
A
So
it
will
take
the
value
10
and
use
it
as
the
parameter
n
in
this
constant
evaluated
function,
and
it
will
give
us
10
generated
values.
So
if
I
add
I,
don't
know
15
the
constant
generic
will
change
and
therefore
we
will
get
15
values.
I
think
you
can,
you
can
guess.
What's
Happening
Here
I
can
show
you
that
I
can
copy
that
one
and
maybe
go
into,
for
instance,
not
that
one
need
a
new
browser
window.
That's
the
wrong
one!
This
one
I
prepared-
let's
put
it
here-
let's
use
the
release
build.
A
This
is
the
rust
playground
and
I'm
going
to
generate
the
assembler
code
here,
let's
give
it
a
second
and
I
will
scroll
down,
I
will
scroll
down,
and
here
you
see
what
I
wanted
to
show
you
here.
We
see
one
two,
three
four
five
constantly
evaluated
inside
the
executable
and
you
can
guess
what
we
see
down
here.
This
will
be
one
two,
three,
four,
five,
six,
seven,
eight
nine
ten.
This
will
be
the
ten
values.
This
is
what
I
wanted
to
show
you.
This
is
really
evaluated
at
compile
time.
A
A
Okay,
I
see
we
have
some
questions.
Let
me
see
the
first
one.
We
have
to
check
the
second
one
I
think
I
clarified
that
and
Alvin
is
asking.
Are
const
functions
not
called
in
a
const
context,
really
added
to
the
compiled
output?
No,
they
are
not.
They
are
just
added
to
the
compiled
output
if
I
call
them
at
compile
time
at
runtime,
I'm.
Sorry
at
runtime,
I
offer
I
stood
your
question
correctly.
If
not,
please,
please
add
the
question
again
or
rephrase
it
a
little
bit.
A
I
hope,
I
understood
it
now.
What
can
we
do
with
this
stuff?
What
can
we
really
do?
Where
can
this
be
useful?
I
created
a
sample
which
is
a
little
bit
longer
and
I
will
copy
it
in
as
a
whole,
because
it
is
a
lot
of
code
and
we
only
have
limited
time.
I
told
you
that
we
will
spend
approximately
half
an
hour
on
this
topic,
so
I
have
to
copy
in
a
little
bit
of
code.
A
This
this
example
is
inspired
by
crates
like
this
one
constra,
and
you
can
take
a
look
at
it.
It's
yeah,
it's
just
an
example,
and
but
but
I
changed
it
I
customized
it
so
I
can
really
describe
what's
going
on
here.
Let's
first
take
a
look
at
this
function.
Here
this
function
is
called
Len
and
what
it
does
it
receives
a
slice
of
strings.
A
We
take
these
strings
and
iterate
over
the
strings.
Unfortunately,
we
cannot
use
an
iterator
here.
Iterators
are
not
supported
inside
of
constant
evaluated
functions,
that's
not
possible,
so
we
have
to
use
something
else.
We
use
slice
the
construction
to
in
a
while
loop,
get
the
current
value
and
the
rest
of
the
values
inside
the
tail
variable,
and
then
we
take
the
length
of
the
current
value
and
assign
the
remaining
here,
which
is
the
right
hand
side
of
this,
while
let
operation
to
the
tail.
So
with
this
we
have
the
full
slice
of
three
strings.
A
Then
we
cut
away
the
first
one.
Second,
slice
keep
cut
away
the
first
one
and
we
we
do
that
and
do
that
and
do
that
until
we
have
no
more
strings
left
inside
of
the
slice
and
we
calculate
the
total
length
of
all
strings
passed
into
the
length
function.
This
is
what
this
method
does
and
because
we
have
written
it
in
that
specific
way,
we
can
decorate
it
with
the
const
keyword
here
at
the
beginning
in
order
to
be
able
to
run
this
method
at
compile
time.
A
Now,
let's
scroll
down
a
little
bit-
and
maybe
you
have
already
guessed
what
I'm
going
to
do
next
I'm
trying
to
build
a
concat
function
that
accepts
a
slice
of
strings
and
then
concatenates
all
these
strings,
but
doing
that
at
compile
time.
Another
runtime.
Let's
take
a
look
at
this
example:
it
takes
the
various
strings
here
as
an
input
parameter
and
allocates
a
buffer
of
adequate
size.
We
simply
take
the
total
length.
We
get
that
again
through
a
const
generic
parameter.
A
A
The
the
slice
deconstruction
again
this
trick
that
we
had
before
in
the
lane
function.
We
allocate
the
appropriate
buffer.
We
had
it
before
so
we
can
simply
copy
all
the
strings
into
the
allocated
buffer.
As
you
can
see
it
here
again,
no
for
Loop,
so
please
excuse
this
code.
It
is
necessary
in
order
to
be
able
to
make
this
method
const
and
at
the
end
we
simply
return
and
help
extract,
which
contains
the
concatenated
version
of
all
the
strings
that
we
passed
in
here.
A
This
is
what
we
do
in
the
concat
method.
So
let's
try
to
apply
that.
Let
me
Zoom,
no
I,
don't
think
I
have
to
assume.
Let's
put
it
that
way,
so
I
can
draw
a
little
bit.
Please
take
a
look
at
this
line
here
here
we
are
declaring
a
slice
of
constant
strings
which
says
hello
world.
Here
we
are
using
a
constant
where
we
call
the
constantly
evaluated
function
Len.
So
we
are
calculating
the
total
length
of
all
provided
strings
in
line
62
inside
of
another
constant.
A
So,
at
the
end
of
the
day,
we
were
able
to
take
all
these
constant
strings
and
concatenate
them
to
receive
a
single
string
at
the
end
and
all
the
magic
is
done
at
compile
time
and
not
at
runtime.
Now,
if
we
hover
over
here
over
this,
you
from
utf-8
unchecked,
it's
obviously
very
important
that
this
method
from
the
rust
core
Library
also
has
the
const
attribute
here.
It
is
also
a
const
function,
because
otherwise
we
weren't
able
to
use
this
function
here
in
a
const
context.
A
It
wouldn't
be
possible,
but
with
this,
that
is
perfectly
fine
and
again
don't
get
me
wrong
here
here
we
can
do
exactly
the
same,
but
this
time
at
runtime.
So
here
you
see
again
our
strings.
We
already
have
the
land
calculated.
We
use
the
concat
method,
which
works
perfectly
fine.
At
runtime,
too.
We
use
the
from
utf-8
unchecked
perfectly
fine
at
runtime
works
like
a
charm,
but
the
const
in
front
of
the
function
allows
us
to
use
the
function
in
a
const
context.
A
Okay,
so
let's
try
that
with
a
little
bit
of
macro
magic.
My
good
friend
Elvin,
convinced
me
that
macros
are
super
useful,
so
I
tried
to
to
build
a
macro
here
that
makes
applying
this
Lane
and
concat
function
in
a
little
bit
a
little
bit
easier.
Alvin
did
a
great
talk
about
macros
last
time,
so
I
tried
to
use
to
apply
this
knowledge
directly
here
in
this
talk,
so
as
you
can
see,
this
is
exactly
what
we
have
done
down
here,
but
just
written
in
a
macro.
A
This
macro
takes
some
strings
as
parameters
as
you
can
see
it
here,
and
we
are
doing
exactly
what
we
did
down
here,
but
that
makes
it
way
more
convenient.
We
can
just
say
my
concat,
which
is
the
macro,
give
it
the
first
string
if
we
want
here.
This
is
a
Content.
This
is
a
const
string,
give
it
here
a
literal
and
give
it
here,
another
const
string,
some
of
you
might
say:
hey.
We
don't
need
that
because
there
is
already
a
concat
macro
built
into
rust.
A
Yes,
that's
possible,
but
this
concat
macro,
that's
built
into
rust,
does
not
accept
here.
Constants
we
have
to
I,
don't
know,
do
something
like
that
and
have
to
add
a
cat,
and
we
have
to
add
the
I.
Don't
know
Mouse
here
yeah
it
works
perfectly
fine.
This
works.
This
is
built
into
rust.
We
don't
need
a
manual
macro
here,
but
I
wanted
to
build
just
for
demo
purposes
a
version
of
a
concat
macro
which
is
capable
of
referring
to
another
constant
just
for
demo
purposes.
A
Just
use
your
favorite
search
engine
to
look
for
crates
that
that
contain
const
functions
that
you
can
use
in
a
const
or
static
context,
but
I
hope
this
example
was
somewhat
useful
for
you
to
understand
what
we
can
achieve
if
we
don't
just
write
trivial
code,
as
we
had
it
before,
but
a
little
bit
more
extra
code,
a
little
bit
more
advanced
code
that
we
can
execute
at
compile
time.
A
Okay,
now
the
latest
and
greatest
versions
of
rust
added
a
bunch
of
extra
things
with
which
I
would
like
to
close
my
clock
today.
Okay,
so
the
first
thing
that
I
would
like
to
show,
you
is
the
possibility
to
to
to
to
to
add
trait
bounds
to
generic
constant
functions.
Let
me
copy
in
a
little
bit
of
code
here
this
one
here
we
are.
This
is
a
little
bit
of
code
here
we
have
the
the
customer
here
and
we
have
a
constant
customer.
Nothing
special
here
works
perfectly
fine.
A
Now
we
can,
as
I,
showed
you
before
Define
constants,
not
just
on
top
level,
but
we
can
also
Define
these
constants
inside
of
the
main
method,
and
here
I'm
doing
something
a
little
bit
different
I'm,
creating
an
array
of
two
customers.
We
have
the
customer
John
and
we
have
the
customer
Jane
and
now
I
would
like
to
write
a
constant
function
that
takes
the
nth
element
from
an
array
and
copies
it
out
here,
and
this
is
how
you
can
do
it.
A
This
is
the
constant
function
and
with
this
constant
function,
we
can
copy
out
the
nth
customer.
Let's
analyze,
this
function,
the
constant
function,
the
implementation
is
Trivial,
that's
not
the
point.
It
gets
an
items
array
here
with
a
constant
generic
parameter
and
it
gets
the
required
index
as
the
second
parameter.
But
the
important
thing
that
you
see
here
is
that
one
this
is
new.
This
is
brand
new.
In
fact,
it
became
possible
stable
in
Rust
161
161
1.61
is
the
version
of
rust.
That
makes
it
possible
to
specify
generic
parameters
for
constant
functions.
A
Chart
sorry
trait
bounds
on
generic
parameters
for
constant
functions.
This
is
new
and
this
is
now
possible.
So
if
you
need
that
take
a
look
at
it,
it
works
like
a
charm.
A
second
example
which
is
new
regards
to
impul
and
doom
traits
to
trade
objects.
So
let
me
get
rid
of
that
example
here
here
we
are,
let
me
get
rid
of
that
example.
Exactly
and
then
I
can
bring
in
a
little
bit
of
code,
I'm
going
to
show
you
what
we
can
do
with
that
now.
A
This
is
a
kind
of
classical
example
when
it
goes
to
polymorphism.
Here
we
have
a
trait
animal,
which
has
a
method
makes
sounds
which
just
Returns
the
sound
that
each
animal
makes
we
have
a
cat
and
we
have
a
dog,
the
cat
says
meow
and
the
dog
says
woof,
pretty
clear,
nothing
to
do
with
constant
evaluation
or
something
like
this.
That's
perfectly
fine.
Now,
let's
add
here
a
helper
method,
and
this
time
it's
a
constant
method,
and
this
is
again
brand
new
for
rust
161..
A
So
if
I
add
that
one,
you
will
see
that
this
method
generates
my
favorite
animal,
which
happens
to
be
a
cat.
We
have
two
cats
and
the
new
thing
is
the
support
for
import.
Here
we
can
return
an
input
trait
from
a
constant
function,
and
that
works
perfectly
fine.
Let
me
show
you
how
that
looks
like
if
we
copy
in
the
code
into
the
main
method.
A
Here,
you
can
see
that
I'm
calling
this
favorite
animal
constant
function
here
and
then
we
can
assign
it,
for
instance,
here
to
to
a
trait
object
and
then
call
it.
This
make
sounds
directly
here,
but
this
favorite
animal
returns
an
import,
an
import
rate
and
that
one
is
new
just
like
returning
an
import.
We
can
also
do
Dynamic
dispatching
to
Dynamic
dispatching
with
something
like
that.
In
this
case,
I
am
implementing
a
method
which
is
called
Animal
by
sound
I,
specify
a
Boolean
value,
saying
whether
the
animal
has
to
be
able
to
purr.
A
If
this
is
true,
this
has
to
be
a
cat.
Otherwise
this
has
to
be
a
dog.
This
is
just
for
demo
purposes,
but
the
point
is
the
return
value
here.
The
return
type
is
a
trade
object,
so
we
are
returning
a
dim
trade
object
from
this
function
and
that
again,
is
a
brand
new
feature
in
the
rust
version.
161.
A
A
A
A
Let
me
show
you
this
example.
Sorry
I
have
to
switch
between
my
visual
studio
codes.
Here
we
are.
Let
me
show
you
something
here
and
in
this
case
I'm
doing
that
one
you
all
might
be
familiar
with
tricks
for
how
to
create
aesthetic.
Mutex
I
just
opened
up
a
stack
Overflow
post,
which
is
very,
very
highly
appreciated
by
many
people,
and
it
shows
how
to
use
a
new
mutex,
a
static
mutex.
A
Let
me
bring
in
the
type
here
and,
as
you
can
see
now,
with
the
nightly
version
of
rust,
which
might
come
with
with
rust,
163
the
new
function
and
various
other
functions
in
the
area
of
sync
types
are
now
const,
so
you
can
see
it
here,
mutex
new
is,
it
can
be
called
in
a
constant
evaluated
context,
and
we
can
do
that
here.
Let's
put
it
that
way,
const.
This
is
what
I
wanted
to
show
you
here.
This
is
the
cons
thing
and
then
we
can
also
use
it.
A
A
This
is
what
I
wanted
to
show
you
here.
We
have
a
static
value.
I
need
to,
of
course,
I
I
told
you
sorry
I'm,
not
that
good
in
typing
and
talking
at
the
same
time.
Of
course,
I
cannot
change
that
to
a
cons,
because
it
isn't
the
cons,
it
should
be
aesthetic.
That's
the
whole
point
here,
but
I
told
you
that
constantly
evaluated
functions
can
be
called
not
only
in
a
const
context,
but
especially
in
a
static
context.
A
So
here
we
use
the
mutex
new
and
it's
possible
because
it
is
now
a
const
if
we
copy
this
one
into
our
rust
playground.
Where
is
the
rust
playground?
I
think
it
is
here
the
here
it
is,
and
we
try
to
run
this
thing.
It
will
probably
not
work
exactly
you
see
that
here
we
have
the
problem
in
the
stable
version.
That
mutex
new
is
is
not
a
constant
function.
A
We
cannot
call
it
here
in
a
static,
constant,
it's
static
context,
but
what
I
can
do
I
can
change
that
one
to
the
nightly
Channel
and
run
it
again,
and
it
will
hopefully
work
just
fine
yeah.
It
worked
just
fine.
This
is
new.
This
is
coming
and
a
lot
of
additional
methods
are
now
const
methods
and
that
make
it
they
make
it
much
easier
to
consume
them
in
a
static,
constant
context.
I
can't
say
context
today
in
a
static
context,
as
I
showed
you
in
the
sample.
So
my
goal
for
today's
talk.
A
Let
me
get
rid
of
my
screen
here.
My
goal
of
today's
talk
was
a
quick
introduction.
What
rust
is
capable
of
doing
I
promised
you
that
I
would
like
to
share.
I
would
like
to
share
my
samples
with
you,
and
I
will
do
that
immediately.
Give
me
a
second
I
will
look
it
up
here
in
my
GitHub
repository,
and
here
it
is
I
will
now
paste
in
the
link
to
the
entire
sample,
including
all
the
comments.
A
All
the
comments
inside
of
the
the
code
and
I
can
also
share
another
file
with
you,
which
I
will
paste
on
the
on
the
Discord
channel.
So
if
you
are
on
the
Discord,
Channel
feel
free
to
grab
the
following
links:
I
go
to
my
Discord
server
here
and
here
it
is,
and
I
will
show
you
that
in
a
second
give
me
a
second.
A
Okay
sent
it
what
I
did
for
those
of
you
who
would
like
to
take
a
closer
look
at
the
samples
that
I
showed
you
in
my
GitHub
in
my
in
the
Discord
server
I
have
created
a
link
to
every
single
sample
that
I
showed
you
today,
so
I
want
you
to
be
able
to
try
all
these
code
Snippets,
you
can
click
on
these
links
and
these
links
will
bring
you
directly
into
the
rust
playground
in
order
to
try
with
it
to
play
with
it
to
see
whether
I
did
something
wrong
or
to
extend
my
samples
or
whatever
you
want
to
do
so.
A
This
is
what
I
wanted
to
share
with
you,
because
I
think
if
this
is
a
beginner
friendly
talk,
it
is
important
for
for
you
to
be
able
to
try
to
play
around
with
these
examples,
and
that
is
essentially
all
that
I
have
for
you
today.
I
promise
you
that
it
will
take
approximately
half
a
minute
to
walk
you
through
some
new
things
that
rust
or
rust
is
capable
of
doing
in
const
contexts.
So
let
me
see
if
we
have
some
additional
questions
here.