►
From YouTube: Traits and You: A Deep Dive — Nell Shamrell-Harrington
Description
Traits are one of the most powerful, but also most difficult parts of Rust to master. Come to this talk for a visual exploration of how traits work - from the most basic to the advanced. It is only through deep understanding of a concept like traits that you can fully harness their power in your every day code. You will walk away with a deep understanding of how traits work, why they work the way they do, and the how and why of using them.
A
Well,
good
afternoon,
everyone
I
want
to
start
off
by
making
sure
to
say
thank
you
for
staying
for
the
last
talk
of
the
conference.
I
know
it's
been
a
long
day
full
of
fantastic
content
and
it
can
be
draining.
So
thank
you
for
still
being
here.
I
want
to
start
off
by
saying
that
traits
are
awesome.
They
are
one
of
my
favorite
things
about
the
Russ
language,
but
that
said
when
I
was
getting
started
with
rust,
traits
were
hard
to
grasp
at
first
did
anyone
else
find
traits
a
little
hard
to
grasp
at
first.
A
Alright,
we
got
a
few
hands
up
here.
That's
good
I
I
think
this
was
particularly
because
I
came
from
a
traditional
object-oriented
language,
which
I'll
talk
a
little
bit
a
little
more
about
a
bit
later,
but
I
have
good
news
for
you
today,
and
that
is
that,
despite
them
being
a
bit
hard
to
get,
my
head
around
at
first
I
have
learned
to
harness
the
power
of
traits,
and
so
can
you
now
before
we
dive
into
traits.
A
A
Alright,
we
got
a
couple
of
us
good
good
that
the
long
the
long
flight
crew
you
can
tweet
at
me
at
at
Nell,
sham
rel
or
feel
free
to
email
me
at
end
sham
rail,
at
Chef,
dot,
IO
and
I
will
put
these
details
back
up
at
the
end
of
this
talk
now,
I've
divided
our
journey
today
into
three
stages.
The
first
is
traits
101.
A
That's
will
cover
the
basics
of
them,
how
you
make
them,
how
you
use
them
and
then
we'll
come
traits
201,
where
we
will
discuss
trait
bounds
and
what
that
allows
you
to
do
and
then
finally
traits
301
will
cover
the
world
of
trait
objects
and
how
they
allow
you
to
take
your
Rusco
to
new
heights.
So,
no
matter
where
you
are
in
your
rust
journey,
I
think
you'll
get
at
least
something
out
of
each
one
of
these
out
of
one
of
these
stages.
A
If
not
all
of
them
and
without
further
ado,
let's
dive
into
traits
101
now
one
of
my
favorite
analogies
for
programming
is
the
game.
Dungeons
&
Dragons,
it's
a
great
model
of
a
system
that
is
defined
by
math
with
very
clear
rules,
but
there
are
infinite
possibilities
for
creating
adventures
and
experiences
within
these
rules.
Now,
speaking
of
Dungeons
and
Dragons
rules,
there
is
one
thing:
I
want
to
make
clear
up
front,
and
that
is
red.
A
If
my
fellow
gamers
in
the
audience
yes
I,
have
simplified
the
rules
of
Dungeons
and
Dragons
for
this
presentation
now,
I
put
this
in
because
when
I
started
writing
this
talk,
I
mentioned
to
some
co-workers,
I'm
thinking
about
using
Dungeons
and
Dragons
as
an
analogy
for
traits
which
led
into
this
very
long
conversation
about
well,
what
part
of
D&D
would
be
the
best
one
for
it,
and
then
someone
piped
in
with
na
Dungeons
&
Dragons
isn't
the
right
system.
We
need
to
talk
about
gurps
or
the
magic
system
when
we're
talking
about
metaphors
for
rust.
A
So
while
that
was
a
fun
conversation,
the
focus
on
this
talk
is
traits
with
Dungeons
&
Dragons
used.
As
a
metaphor,
it's
not
meant
to
be
a
literal
interpretation
of
the
rules
of
dodges
and
Dragons
fifth
edition
now,
with
that
on,
though,
out
of
the
way,
let's
go
ahead
and
begin
in
Dungeons
&
Dragons,
you
start
a
game
by
creating
a
character,
and
the
first
thing
you
select
about
that
character
is
its
race.
A
There
are
nine
to
choose
from,
but
in
this
talk,
let's
first
focus
on
these
four
a
dwarf
and
elf,
a
half-orc
and
a
human.
So,
let's
represent
these
by
creating
strips
in
rust.
Trucks
are
fantastic
way
to
name
and
package
together
different
values
into
a
meaningful
group,
so
we
create
a
structure
for
a
dwarf.
We
create
one
for
an
elf
right
now.
A
The
only
feel
we're
going
to
worry
about
is
the
name
of
the
character
and
then,
let's
add
in
one
for
a
half
hour
and
another
struct
for
a
human,
so
we've
defined
the
racism,
Dungeons
&,
Dragons
s,
trucks.
Now,
when
I
want
to
make
a
character,
I
can
create
an
instance
of
one
of
these
trucks.
So
if
I
want
to
create
a
dwarf,
I
create
an
instance
of
the
dwarf
struct
and
for
now
we'll
name
it
Nell
dwarf.
A
So
now
that
we
have
a
basic
character,
we
need
to
add
certain
traits
to
it,
and
every
D&D
character
has
six
core
traits
strengths:
that
steady
Constitution,
intelligence,
wisdom
and
charisma.
Now,
for
now,
let's
just
focus
on
Constitution.
We
have
our
dwarf
character.
Now
now
we
need
a
way
of
giving
it
a
constitution.
So
to
do
that,
let's
make
it
Freight,
let's
make
a
public
tray
or
call
it
constitution,
and
in
this
trait
we
define
one
function:
Constitution
bonus
notice
that
we
don't
define
how
this
function
is
carried
out.
A
We
just
define
the
name
of
it,
what
we
expect
pass
to
it
and
the
expected
output,
which
in
this
case
is
a
u8
number.
So
now
that
we
have
the
trait
defined,
we
need
to
implement
this
constitution
trait
on
our
dwarf
struct.
It
should
have
all
the
functionality
of
this
trait
but
be
able
to
override
parts
of
it
as
needed.
So
this
is
how
we
implement
the
Constitution
trait
for
our
dwarf
struct.
A
Now,
along
with
implementing
this
basic
trait
on
it,
we
also
need
to
define
how
this
constitution
bonus
function
works,
and
we
remember
when
we
made
this
trait.
We
left
exactly
how
this
function
works
up
to
whatever
struct
implements
this
trait.
So
to
implement
this,
we
need
to
understand
that
the
Constitution
bonus
for
a
dwarf
is
two
that
is
common
to
any
character
that
happens
to
be
a
dwarf.
So
that
means,
if
you're
doing
some
sort
of
action
where
you
need
to
roll
the
dice
and
score
a
certain
Constitution
score.
A
If
you
have
a
dwarf,
you
can
always
add
two
to
it,
just
by
virtue
of
being
at
that
race.
So,
let's
add
this
in
and
let's
implement
Constitution
bonus
for
our
dwarf
and
we're
gonna
say
it's
going
to
return
two
whenever
Constitution
bonus
is
called
on
an
instance
of
the
dwarf
struct.
So
let's
see
this
in
action
here,
I've
got
my
character.
A
It's
a
dwarf
with
the
name
of
Nell
dwarf
and
when
I
call
Constitution
bonus
on
this
character,
it
will
return
two,
so
I
could
create
more
implementations
of
the
struct,
more
dwarf
characters
and
each
one
of
them
will
return
two
whenever
I
call
Constitution
bonus
on
them.
Now
that
covers
one
of
our
socks.
Let's
take
a
look
at
one
that
might
have
a
different
Constitution
bonus,
and
that
is
the
half-orc.
When
I
want
to
add
a
constitution
to
the
half-orc
struct,
I
first
implement
it
and
then
remember.
A
I
also
need
to
define
the
Constitution
bonus
for
that
1/2
or
extract,
which
is
different
from
a
Dwarfs
Constitution
bonus.
The
Constitution
bonus
for
a
half-orc
is
1.
That
means
anytime,
you
do
a
dice
roll
and
you
need
to
score
a
certain
Constitution.
You
can
always
add
1
if
your
character
is
a
half-orc,
so
they
have
this
base.
Constitution
bonus
and
I
go
ahead
and
implement
that
on
my
implementation,
this
traits
for
half-orcs
and
this
time
it
will
return
1.
A
Now,
let's
see
this
in
action,
let's
create
a
half-orc
called
nell
ork
and
let's
call
constitution
bonus
on
it.
So
when
I
call
Constitution
bonus
on
any
instance
of
the
half-orc
struct,
it
will
return
1
now,
there's
two
more
races.
We
need
to
implement
a
constitution
for
the
elf
struct
and
the
human
struct,
and
these
two
actually
have
the
same
Constitution
bonus
if
you're
playing
1/2
if
you're
playing
a
human
or
a
half
elf
or
an
elf,
your
Constitution
bonus
will
always
be
0.
So
let's
go
ahead
and
implement
this.
A
We
implement
Constitution
for
the
elf
and
implement
Constitution
for
human.
Now
we
could
do
it
like
this,
defining
the
Constitution
bonus
for
both
of
them
as
0,
but
this
is
pretty
darn
repetitive
and
it's
not
not
really.
What
I
like
to
see
in
my
programming,
something
to
know
is
that
most
races
have
a
Constitution
bonus
of
0.
It's
an
exception
when
something
has
a
Constitution
bonus
other
than
0
in
Dungeons
and
Dragons.
A
So
let's
go
ahead
and
implement
0
as
the
default
for
a
Constitution
bonus,
and
we
do
that
by
going
back
to
where
we
defined
our
traits
and
that
Constitution
bonus
function,
and
we
say
it
will
return
0.
So,
unless
a
struct
that
implements
this
trait
overrides
the
Constitution
bonus,
it
will
always
return
0.
So
let's
see
this
in
action
and
let's
implement
this
on
our
elf
and
our
human
struct
and
I'm
going
to
need
to
implement
the
traits
I,
don't
need
to
define
how
Constitution
bonus
works,
be
cause
I'm
using
the
default
functionality
of
it.
A
A
Now,
let's
create
a
human
and
the
human
I'm,
just
gonna
call
Nell,
because,
although
if
you
can
tell
I
play
Dungeons
and
Dragons
whether
I
was
human
was
up
for
debate
in
middle
school,
I
am
I,
pretty
sure
I'm,
a
human,
so
I'm,
just
gonna
call
my
human
Nell
and
then
I
call
Constitution
bonus
on
it
and
it
returns
zero.
So,
at
this
point
yay
we
have
a
trait
and
we
have
a
default
functionality
for
that
trait
for
the
Constitution
bonus
function,
but
we
can
always
override
it
if
we
need
to
now.
A
That
brings
us
to
the
end
of
traits
101.
So
let's
dive
right
into
traits,
201
trait
bounds
and
again
we're
using
our
Dungeons
&
Dragons
metaphor
and
this
time
I'm
going
to
add
another
race.
We've
got
our
four
core
races
right
now.
Let's
add
in
a
half
elf.
Now,
as
you
can
see
the
world
of
Dungeons
and
Dragons,
it's
a
pretty
diverse
world.
There's
people
with
lots
of
different
backgrounds
and
different
races.
They
need
a
way
to
communicate
and
part
of
creating
your
character
is
defining
how
that
character
can
communicate
with
the
outside
world.
A
So
every
race
comes
with
some
core
languages
that
it
already
speaks,
dwarfs
be
common
and
dwarvish
elves
be
common
in
elvish
and
half
elfs,
also
speak
common
and
elvish.
So
let's
focus
on
the
elvish
language
for
right
now
and
let's
make
that
a
trait
we
can
implement
on
our
structs,
so
I
create
my
elvish
traits,
then
I
implement
it
on
both
my
elf
struct
and
my
half
elf
struct.
Both
of
these
races
speak
elvish.
A
Now,
let's
make
a
function
separate
from
our
traits
for
allowing
our
for
allowing
a
character
to
take
the
action
of
speaking
elvish
and
let's
define
a
function,
called
speak
elvish
and
we
say
it
will
return
a
string
and
let's
say
it,
returns
the
string.
Yes,
so
right
now,
this
function
can
be
called
on
any
with
no
arguments.
So,
let's
narrow
it
down
a
bit
and
let's
say
that
we
accept
an
argument
called
character,
but
that
character
does
not
need
to
be
a
specific
type.
A
It
can
be
any
kind
of
it
can
be
any
of
our
struct
that
we've
already
created.
So
we
have
a
generic
type
that
we
accept
as
an
argument
to
this
function,
but
we
will
only
accept
those
types
that
implement
the
elvish
traits.
So
if
I
run
this
method,
I
have
my
elf
and
I
run
the
method.
The
method
speak,
elvish
passing
it
in
my
elf.
As
an
argument
member
my
elf
implements
the
elvish
trait,
so
it
will
return.
A
Yes,
now,
let's
see
this
on
the
half
elf,
pretty
much
the
same,
I
create
my
half
elf
and
then
I
call
my
speak.
Elvish
function
and
pass
it
in
that
half
elf
as
a
argument
and
because
my
half
elf
does
implement
elvish,
it
returns.
Yes,
now
what,
if
I
pass
it
a
different
type
of
character?
Let's
say:
I
create
a
half-orc
and
then
I
call
my
speak.
Elvish
function
and
I
pass
it
in
my
half
orc
as
an
arguments.
A
Well,
as
we
can
see,
the
elvish
trait
is
decidedly
not
implemented
for
a
half
or
CAF
orcs
do
not
speak
elvish
by
default.
So
in
this
case,
when
I
try
to
compile
my
code,
it
will
return
an
error
on
compile
letting
me
know
that
the
trait
elvish
is
not
implemented
for
a
half-orc.
Now,
one
of
the
things
I
like
about
rust
is
that
so
many
errors
like
this
return
on
compile.
It
doesn't
give
me
a
chance
to
try
to
run
my
code
like
this
and
possibly
have
a
catastrophic
error.
A
A
You
can
have
multiple
traits
that
you
can
I
keep
as
criteria
for
a
function
accepting
so
let
bring
that's
the
end
of
traits
201.
Now,
let's
move
into
traits
301
a
trait
objects,
and
this
is
the
really
cool
stuff.
So
this
took
me
the
longest
to
get
my
head
around
now
in
traditional
object-oriented
languages,
I
came
to
rust
from
Ruby.
Those
of
us
who
came
from
object-oriented
languages
are
probably
familiar
with
this
concept
of
an
object.
A
An
object
holds
both
the
data,
the
characteristics
of
an
object
and
the
behavior,
the
methods
you
can
call
and
that
object
to
manipulate
and
use
it
now
rust.
Does
this
a
little
differently
in
rust,
we
usually
keep
the
data
about
something
in
an
enum
or
struct,
and
we
keep
the
behavior
in
a
traits,
so
the
data
and
behavior
are
kept
separately.
Now,
that's
one
of
the
powers
of
rust.
We
can
kind
of
mix
and
match
behavior
as
we
need
it
in
our
code,
but
trait
objects
are
also
a
little
different.
A
Trait
objects
behave
more
like
traditional
objects,
in
that
they
contain
both
data
and
behavior,
but
it's
still
a
little
different
from
a
traditional
object.
The
way
a
trait
object
works
is
entry.
Object,
consists
of
a
pointer
to
a
value
on
the
heap,
the
advantage
of
storing
the
value
itself.
The
data
on
the
heap,
while
keeping
a
pointer
to
it
in
the
trait
object,
is
that
even
if
the
size,
the
value
on
the
heap
varies,
the
size
of
that
pointer
will
always
be
the
same.
A
It
makes
it
much
more
predictable
for
using
this
and
dealing
with
memory.
So
that's
the
data
of
a
trait
object.
The
behavior
of
a
trait
object,
how
you
can
use
it
and
manipulate
it
comes
from
a
traditional
traits
now,
although
a
trait
object
does
point
to
data
on
the
heap,
one
thing
is
critical
to
understand
is
that
you
cannot
add
data
to
a
trait
object.
This
is
the
key
to
understanding
the
purpose
of
trait
objects
and
how
to
use
them
we're
pointing
to
the
data
at
one
specific
point
in
time.
A
A
If
I'm
running
cast
on
it,
then
define
whatever
you
need
to
do
to
cast
a
transmutation
that
might
be
different
from
casting
a
can
trip
and
then
finally,
I
do
the
same
thing
for
enchantment
I,
implement
it
in
detail
how
enchantments
are
cast
and
then
I
do
the
same
thing
for
the
necromancy
struct.
So
now
each
of
these
types
of
spells
can
be
cast,
but
they
are
left
to
define
anything
specific
about
them.
That
is
required
to
cast
that
specific
type
of
spell.
A
So
it's
common
in
D&D
to
acquire
a
whole
bunch
of
spells
as
you
level
up
and
it's
likely
that
my
wizard
or
sorcerer
will
need
a
place
to
keep
them
to
kind
of
store
them,
as
we
continue
on
our
journey
throughout,
whatever
magical
world
were
exploring.
So
where
do
we
keep
spells
in
a
spell
book,
of
course,
where
we
can
easily
find
them
and
work
with
them?
A
The
type
of
this
field
looks
a
little
weird
in
the
beginning,
so
let's
go
ahead
and
break
it
down
bells
feel
the
spells
field
is
a
vector
and
vectors
are
ways
to
group
objects
of
a
certain
type
and
the
type
we're
grouping
in
our
vector
for
spells
is
a
box
and
a
box
and
rust
is
a
pointer
to
a
value
on
the
heap.
Does
this
look
familiar?
A
The
beautiful
thing
about
about
using
a
box
that
contains
that
that
pointer
to
the
value
on
the
heap
is
again
the
boxes
size
is
consistent
in
memory,
no
matter
what
the
size
of
the
value
on
the
heap
is.
Now
we
also
four
boxes.
We
usually
only
contain
values
of
a
certain
type,
and
in
our
case
we
say
that
we
can
contain
any
type
that
implements
the
caste
trait.
It
doesn't
matter
what
type
it
is
as
long
as
it
implements
that
particular
trait.
A
A
A
Well,
we
can
do
this
in
rust
by
defining
some
behavior
for
our
spell
book
structs
through
this
implementation
block,
and
what
we're
going
to
do
is:
let's
implement
a
function
called
run
and
this
function
will
take
every
spell
in
a
spell
book
and
iterate
over
it
and
cast
though
each
individual
spell
one
right
after
the
other
remember,
the
types
of
spells
themselves
will
define
how
they
are
cast,
but
the
thing
they
all
have
in
common
is
that
they
implement
this
cast
trait
and
define
that
cast
function.
So,
let's
look
at
a
visual
representation
of
this.
A
We've
got
our
wizard
or
sorcerer
or
whatever
magical
class.
You
prefer
and
he's
going
to
call
run
on
his
spell
book
and
that's
gonna
cast
every
spell
in
that
spell
book,
no
matter
what
type
it
is
right
after
the
other.
So
we've
seen
a
visual.
Now,
let's
see
this
in
code,
let's
create
a
new
instance
of
our
spellbook
struct
and,
let's
define
the
spells,
remember
the
spells
field
is
a
vector
and
that
vector
contains
boxes
that
contain
different
types
of
spells.
A
Again,
it
doesn't
matter
what
type
of
spell
it
is
as
long
as
it
implements
that
particular
cast
trait
so
when
I
want
to
cast
all
of
these
one
after
the
other
I
can
call
the
run
method
on
my
instance
of
the
spell
book
struct
and
they
will
all
be
cast
from
this.
One
function
in
whatever
way,
is
required
for
that
type
of
spell
to
be
cast.
So
what
this
highlights
is
that
trait
objects
are
great
for
heterogeneous
collections,
where
we
want
to
be
able
to
have
objects
of
different
types
stored
in
the
same
place.