►
From YouTube: Rust Cologne: A generalist's view of traits
Description
https://media.ccc.de/v/rustcologne.2018.09.generalists-view-traits
People coming from other languages can find a lot of familiar aspects in Rust’s traits:
Traits are somewhat like interfaces, and also somewhat like type classes, and also like… classes?
This talk will try to give you an overview of what traits are and how you can use them to make your code more concise and flexible.
Pascal Hertleif
A
My
name
is
Pascal
and
these
days
I
do
web
development
related
stuff,
but
also
rust,
and
one
of
the
co-organizers.
A
serious
cologne
and
my
handle
on
various
places
on
the
internet
is
killer
cup
and
if
you
want
to
read
some
rambling
about
some
stuffs,
that
also
often
includes
rust,
go-to
deterministic,
dot
space
first
off
a
warning
through
these
slides
together
and
all
I
could
come
up
with
this
code,
so
lots
of
code,
but
also
please
please
please
interrupt
me
when
you
have
questions
as
Florian
just
mentioned.
A
Please
use
the
mic,
so
why
are
we
even
talking
about
rates?
Let's
first
take
a
step
back
and
talk
about
what
you're
even
doing
when
we
are
programming
and
most
of
the
time
the
things
we
are
doing
is
dealing
with
data
and
doing
some
stuff
with
it.
So
let's
do
some
stuff
with
us.
So
this
example
I
hope
you
can
all
read
it
is
a
function
called
make
true.
It
takes
as
input
parameter,
string
slice
and
gives
you
back
a
string,
and
how
do
you
make
stuff
true?
A
Do
you
shout
them,
obviously,
by
adding
exclamation
marks?
That's
yet
okay,
so
this
is
good.
We
can
now
make
string.
Slice
is
true,
and
this
is
a
very
useful
function,
because
we
have
string
slices
in
many
various
forms,
but
also
we
maybe
want
to
represent
our
data
in
a
more
domain-specific
way.
For
example,
if
we
are
actually
dealing
with
facts,
we
may
not
want
to
take
a
string
slice,
but
a
reference
to
affect
this
fact
is
indeed
justice
structure,
which
has
one
field
context,
and
it
is
a
string.
A
A
How
did
you
add
data
behavior
to
data
well
in
most
programming
language?
There's
the
concept,
that's
called
a
method
and
you
may
be
asking:
how
do
you
write
a
method
in
rust?
The
answer
is
rather
simple:
you
write
input
and
the
name
of
your
data
type
open,
curly
brackets,
and
then
you
write
some
function,
maybe
copy
paste
in
the
function.
You
just
saw
on
the
slide
before
this
one
and
close
parentheses.
A
You
actually
are
decorating,
you
can
access
this
data
using
a
form
of
self
as
first
parameter
self
is
very
explicitly
set
in
rust
and
not
implicitly
given
by
a
keyword
like
this,
as
in
JavaScript
or
Java,
for
example-
and
you
may
be
wondering-
am
form
of
self
yes
form
of
self,
because
in
rust
you
actually
have
to
annotate,
which
kind
of
self
do
you
want?
Do
you
want
a
reference
to
self
that
you
can
only
read?
A
A
Okay,
let's
look
at
another
implementation
of
make
true.
So
here
we
take
the
mutable
reference
to
our
effect
and
we
don't
generate
a
new
string
to
put
in
a
new
effect.
We
actually
just
change
our
effect,
so
it's
now
an
alternate
effect
or
something
and
the
natural
implementation
would
be
to
take
this
fact
as
in
itself,
without
taking
a
reference
and
returning
it
in
a
change
form.
What's
the
difference
that
you
can
no
longer
use
original
effects
you
put
into
this
okay,
now
we've
covered
data
and
how
we
can
add
behavior
to
it.
A
But
I
mentions,
as
this
talk
was
actually
what
writes
and
you
may
be
asking
assistance
right
and
yes,
it
is,
but
it
doesn't
have
a
name
and
we
can
give
it
a
name.
We
can
call
the
straight
truth
and
write
this
a
function,
signature
or
the
method
signature
in
this
case
it
is
in
this
case
reference
to
self,
as
we
saw
on
the
first
example
and
return
something
some
self.
Okay,
we'll
come
back
to
this.
A
A
Well,
the
most
obvious
one
is
that
we
now
can
name
these
concepts
and
we
can
share
as
a
behavior
between
different
data
types
and
also
we
can
abstract
over
this
behavior
and
the
way
we
do
this
is
by
writing
a
implementation
like
this.
This
is
a
function
called
print
news
and
as
an
input
parameter,
it
takes
a
reference
to
a
slice
of
tea
like
some
list
that
contains
T's.
But
what
is
T
t
is
just
a
placeholder,
the
type
parameter,
and
it
has
some
constraints
on
it.
A
As
you
can
see,
right
after
the
function
name
in
the
angular
brackets
t,
:
truce
means
we
have
some
placeholder
called
T
and
we
need
any
type
of
data
that
implements
the
trade
truce
we've
just
written
so
function.
We
can
do
some
stuff
with
this
facts
list
and
we
can
at
least
make
them
true,
because
we
know
any
type
in
this
list
implements
truth,
but
actually,
if
you
look
at
it,
this
does
not
compile,
because
we
are
not
only
making
facts.
True,
we
also
printing
them
and
indeed
Russ,
see
the
Russ
compiler
tells
us.
A
T
does
not
implement
standard
format
display,
because
we
have
no
idea
how
to
actually
display
facts.
This
error
message
is
actually
quite
explicit
about
it.
He
cannot
be
former
that
was
it
default
formatter.
It
has
some
helpful
ideas
like,
for
example,
at
the
trade
display
is
not
implemented
and
that
we
can
do
other
stuff
with
it,
but
also
that
it
is
required
by
format
and
format
is
actually
some
things.
Print
macro
calls
internally,
that's
all
good,
but
why?
A
A
If
you
also
wants
to
print
it,
we
need
to
add
another
implementation,
and
indeed
we
can
just
say
so,
like
we
can
import
standard
from
it,
the
module
that
contains
the
display,
crate.
Let's
write
and
say:
ok,
our
team
needs
to
implement
truth
and
also
display
this
works,
assuming
that,
given
our
example
copy
form,
we
also
add
this
implementation
of
another
traits.
A
Interesting
fact:
if
we
go
back
to
this,
the
T
Colin
truths
plus
format
display.
Yes,
many
not
the
most
interesting
information
you
have
in
this
particularly
a
place
right
after
the
function.
Signature.
So
there's
another
way
of
writing
it,
and
this
is
where
of
syntax.
You
can
write
T,
:,
throughs
and
display
right
after
the
function
name
also,
you
can
write
just
the
T
after
the
function
name
and
then
after
the
function,
signature
put
aware
and
say:
trues
plus
display.
Just
so
you
don't
get
confused.
This
is
absolutely
equivalent
syntax.
A
By
the
way,
talking
of
syntax,
we
can
also
add
placeholders
in
other
places,
for
example
in
our
implementation
block.
We
can
say:
okay,
we
want
to
implement
this
trait
for
this
type,
but
with
the
placeholder
in
the
type
as
well
like
this
implementation
block
is
valid
for
all
vectors,
where
the
items
implements
a
debug,
trait
and
debug
is
basically
as
a
machine,
readable
version
or
the
human
debuggable
version
of
the
display
crate
right.
A
Okay,
this,
basically
all
you
need
to
know
if
you
want
to
write
methods
in
traits
and
implement
them
for
your
types,
but
you
can
also
implement
other
stuff.
You
can
add
other
things
to
your
traits
and
we
just
need
to
take
one
more
step
back
and
talk
about.
What
even
is
that's
right.
It
writes
the
connection
of
associated
items.
It's
the
most
basic
one
associated
functions
is
what
we've
seen
all
along
its
methods,
but
Sarah
also
associated
types.
So
consider
this
example.
A
There
are
some
iterators
right
and
all
you
need
to
do
to
implement
an
iterator
is
to
provide
the
next
function.
There
are
way
more
functions
on
iterators
and
I'm
showing
here,
but
next
is
the
most
important
one
and
litterateur
works
like
this.
Do
you
have
something
that
wraps
your
data
and
if
you
call
the
iterators
next
function
on
it,
you
get
a
new
value
like
from
your
data
from
your
list,
for
example,
where
you
have
a
vector
of
integers
and
you
get
the
next
one,
for
example,
to
implement
a
for
loop.
A
A
But
if
you
write
an
implementation
for
NASA
datatype,
that
may
not
be
generic,
you
can't
just
look
at
the
signature
of
the
datatype
and
say:
okay.
Obviously,
this
has
to
be
also
a
T,
because
there
is
no
T
so
with
associated
types.
You
can
specify
what
you
want
to
call
this
type
that
you
want
to
return,
because
this
type
is
not
a
generic
type
over
your
data,
it's
the
generic
type
that
it
becomes
concrete
in
your
implementation
block.
A
So,
for
example,
if
I
have
a
data
type
called
for
integers,
that
gives
me
you
guessed
it
for
integers.
My
return
type
is
an
integer
if
I
had
the
data
type
that
contains
the
file
and
they
won't
read
lines
from
it.
My
return
tab
in
this
iterator
would
be
string,
for
example,
so
associated
types
is
a
way
to
give
a
name,
and
also
in
the
iterator
different
sorry
I'm
in
the
trait
definition.
To
give
the
constraints
these
types
need
to
fulfill.
A
A
This
is
associated
items,
there's
one
more
item.
We
can
cover
it's
kind
of
in
each
case,
but
I'm
just
showing
it
associated
constants.
For
example,
if
you
have
a
super
lock
trait
and
you
want
to
prefix
each
lock
message
with
a
specific
string
that
is
specific
to
your
type,
but
you
don't
want
to
have
a
message
that
always
returns
the
type
name.
You
can
write
a
constant,
so
this
is
just
a
constant
set
lives
alongside
your
straight
implementation.
A
A
There
are
other
interesting
things
you
need
to
know
about
traits
to
really
get
started.
For
example,
say
you
write
a
trait
and
you
have
a
function.
That's
called
clone.
You
might
not
know
this,
but
the
standard
library
also
has
a
trade
with
a
function
called
clone.
What
do
you
do
if
you
use
the
regular
syntax
of
my
name?
Dot
clone?
You
have
no
idea
which
message
you're
actually
calling.
A
So
there
is
a
syntax
to
specify
which
trait
you
actually
want
to
call,
and
it's
angular
brackets
my
data
type
as
the
trait
I
want
to
treat
it
as
colon
colon
method
name.
You
can
also
write
in
this
case
truths.
Colon
colon
make
true,
because
you
know
that
I'm
only
supplying
one
parameter
effect,
which
determines
that.
Actually,
my
implementation,
for
fact,
is
going
to
be
called.
Yes,
please
go
ahead.
A
B
It
would
be
actually
most
well
traits
with
famous
as
implemented
on
one
datatype.
Would
they
be
like
default
method
that
will
be
called?
I
would
just
be
a
compile
time
error
if
you
call
it
as
two
regular
syntax,
so
you're
also
always
required
to
use
the
special
syntax
in
that
case
or
what
would
happen
very.
A
A
All
right,
one
area
we
haven't
covered
yet
is
what
traits
are
also
used
for
that.
You
may
not
know,
because
if
you
write
one
plus
two
actually
it
expands
to
an
implementation
of
the
ad.
It's
right
that
isn't
the
standard
operators
module.
The
at
trade
is
very
simple.
It
actually
only
gives
you
an
ADD
method,
but
this
also
means
that
you
can
write
your
own
implementation
of
ad.
A
Another
interesting
edge
case,
it's
right
that
has
basically
no
content
whatsoever
and
it's
still
useful.
These
are
called
market
rates
and
you
can
use
them
to,
as
a
name
suggests,
mark
stuff,
for
example,
if
you
wanted
to
say
you
only
care
about
the
specific
set
of
types
that
all
happen
to
implement
this
play,
but
also
needed
to
be
fancy.
A
This
may
sound
weird
at
first,
but
it's
actually
how
rust
implements
the
notion
of
sender,
bill
and
syncable
types.
If
you
wanted
to
use
a
specific
data
type
in
multiple
threads,
the
compiler
might
give
you
a
message
like
now.
You
can
do
this
because
this
type
doesn't
implement
send,
or
this
type
doesn't
implement.
Sync,
these
traits
do
nothing
but
require
you
to
say
this
type
is
actually
send
able
across
threads,
because
a
compiler
can't
prove
that
this
type
actually
is
sandable
over
threats.
To
give
it
a
marker.
A
A
C
A
Is
also
a
very
good
question:
I
don't
have
a
precise
answer,
I'm
fairly
certain
that
they
are
implemented
in
the
core
library
and
marked
as
language
items,
so
you
can
go
either
way
and
say:
ok,
this
is
actually
a
like
language
specific,
but
also
it's
in
the
core
library
and
I
could
replace
it,
I'm,
not
sure
for
drop.
But
for
many
other
things
there
are
flagged
as
language
items.
D
A
E
Thank
you,
so
you
had
this
example
of
the
trade
where
you
had
a
function
that
returns
a
self.
Yes,
this
looks
kind
of
like
a
generic,
so
where's
the
difference
between
this
and
a
trade
generic.
A
Some
kind
of
placeholder
for
the
type
you're
actually
implementing
your
trade
form
in
this
case
you
know
it's
a
32
in
a
more
generic
case.
You
don't
know,
what's
the
type
actually
yes
and
in
our
trade
definition,
you
also
don't
know
yeah,
you
don't
know
what
type
you're
implement
what
type
your
implementation
is
currently
trying
to
deal
with,
but
you
don't
want
to.
A
E
A
E
A
What,
if
you
want
to
parameterize
a
trait
but
only
ever
implemented
for
one
specific
return?
Tab
like
if
iterator
was
generic
over
its
return
types?
You
could
write
multiple
implementations
of
iterator
for
vector
ax,
for
example,
but
using
Associated
types
which
are
not
parameterised
themselves
in
the
implementation
and
signature.
You
can
only
ever
provide
one
implementation
of
iterator
for
a
vector,
it's
an
additional
constraint.
Basically,
if
that
makes
sense,
I.
B
B
But
you
may
want
to
be
able
to
put
different
types
types
in
there
like,
for
example,
as
if
you
would
want
to
have
an
vector
of
displayable
objects,
but
you
can't
really
do
that
in
respeakers,
2f
and
yet
that
that
they
have
may
have
different
sizes
and
therefore
you
can't
really
put
them
in
a
vector,
but
a
third
possibility
of
do
to
do
something
like
that
to
create
a
vector
of
displayable
objects.
There.
A
A
Not
without
being
very
explicit
about
what
you're
doing,
though,
because
what
you're
doing
is
taking
a
type
and
throwing
away
everything
you
know
about
it,
except
for
that
it
implements
display,
and
then
you
put
it
in
a
box
and
say:
oh
now,
I
have
a
pointer
to
something
that
I
know
implants
display,
and
then
you
can
do
this
with.
Basically
every
type
you
know
influences
play
and
put
all
of
these
boxes
into
a
vector.
So
then,
you
have
literally
the
type
back
of
box
of
display.
B
A
A
A
A
A
A
There
are
more
variants
of
this.
You
can
also
write
a
vector
of
playable
pointers,
for
example,
if
you
don't
want
to
actually
box
them,
but
just
want
to
have
a
reference
start
on
a
vector
to
something
you
know
is
displayable
the
implementation
detail.
Processors
may
be
the
same.
You
do
in
most
other
languages,
there's
the
V
table.
This
is
a
virtual
virtual
function
for
table,
so
this
box
is
actually
ponder
to
your
data
and
the
pointer
to
a
table
that
contains
all
the
methods
display
is
implemented.
Is
in
this
containing
basically.
F
C
A
Good
question:
so
if
you
do
this
or
actually,
if
you
do
any
of
this
and
use
it
like
this,
you
get
static
dispatch.
So
at
compile
time
there
is
a
caller
function
to
a
call
statement
to
well-known
function
that
is
specific
to
the
type
you
are
using.
For
example,
this
is
one
implementation
of
the
make
truthful
make
true
function.
This
is
another
one,
and
both
of
them
are
somewhere
in
your
machine
code
and
depending
on
what
you're
calling
make
true
on
the
correct
one
gets
selected.
So
this
also
true
for
very
generic
code.
A
So,
for
example,
if
you
have
this
is
implemented
for
every
T,
where
T
is
cloneable,
for
example,
and
T
in
your
case
is
a
string.
Concrete
implementation
for
clone
will
be
generated
for
strengths.
This
is
called
monomer
physician.
So,
instead
of
having
one
implementations
that
cover
us
all
cases,
the
very
specific
implementation
will
it
be
generated
for
the
specific
case
of
types
you
provide.