►
From YouTube: PDXRust May 2016: Mike Cooper - Traits and Types
Description
PDXRust
More on http://www.meetup.com/PDXRust/events/229349425/
Help us caption & translate this video!
http://amara.org/v/2Fi4/
A
Little
situations
because
there
are
so
many
bikes
in
the
world,
so
I'm
going
to
be
talking
about
the
trait
system
and
like
how
it
intersects,
with
the
type
system,
I
kind
of
assume.
You
know
a
little
bit
of
rust.
We
aren't
going
to
cover
the
the
very
basics,
but
if
anything
is
confusing,
please
interrupt
me
and
ask
questions.
A
Raise
your
hands
is
good,
so
first
I
want
to
talk
about
why
rusts
type
system
is
worse
than
knowing
the
type
system
is
basically
the
skeleton
of
the
language
it
underlies
some
of
the
most
important
features.
It's
how
we
get
our
type
safety,
a
memory
safety.
It
also
does
error,
handling
and
flow
control.
All
the
operators
are
implemented
with
the
type
system
and
the
trade
system,
and
all
of
the
memory
management
is
done
with
traits.
So
this
is
a
very
important
part
of
the
language
to
understand
so
a
brief
overview
of
rust
types.
A
Everything
you
must
have
the
type,
even
if
it
doesn't
look
like
it
does
so
this
program
here
there
are
no
types
it
expressly
written
and
that's
because
rust
will
automatically
detect
many
types
within
a
function.
So
the
same
function
with
its
variables,
annotated
with
their
types,
looks
like
this,
and
it's
like
this.
What
the
compiler
would
have
figured
out
well,
I,
don't
know
that
would
have
given
counted
as
an
I-32,
but
sometimes
we
need
to
tell
the
compiler
what
we're
talking
about
the
built-in
types
for
like
come
with
the
language.
There's.
A
This
is
an
incomplete
list.
There's
you
basically,
true/false
characters
are
a
30
I
think
there
are
32-bit
value,
that
represents
a
Unicode
code
point
and
there's
a
numeric
types
which
we'll
cover
in
a
second
arrays
and
slices
and
tuples,
which
all
kind
of
look
the
same,
but
are
all
subtly
different.
We
aren't
really
going
to
get
into
the
differences
of
those
because
they're,
subtle
and
very
complicated
and
then
string
and
a
string.
A
Reference
are
sort
of
like
array
and
slice
ins,
the
mara
types
there's
quite
a
few
of
them,
and
this
is
I'm
kind
of
how
they
are
related.
Various
different
integer
sizes,
both
signed
and
unsigned,
and
a
few
floating-point
number
is
the
one
interesting
one
here
is
the
eye
size
and
you
size.
Those
are
the
Machine
type,
which
means
on
a
32-bit
system.
They
are
32-bit
bits
wide
on
64-bit
system,
they're,
a
64-bit
one.
This
is
also
the
size
of
pointers
and
like
array,
references
so
to
talk
about
traits
and
types.
A
B
A
A
I
could
be
wrong,
so
Russ
has
the
ability
to
make
functions
like
this.
It's
actually
a
little
clever.
We
can
use
what's
called
generics
and
it's
a
generic.
You
say:
I,
have
it
sorry.
This
is
another
out-of-sync
slide:
I
have
a
function
called
add,
which
is
generic
over
the
type
T
I
want
to
take
two
parameters,
a
which
is
a
T
and
B,
which
is
a
T
I'm,
going
to
return
something
of
the
same
type,
then,
as
the
other.
A
B
A
A
The
traits
are
how
we
constrain
types,
how
we
limit
it
from
everything
to
just
some
types
and
in
our
example,
we
need
to
constrain
T
to
only
things
that
can
be
added.
We
do
that,
like
this
I'm
sorry-
and
this
is
the
ad
trait-
this
is
a
very
simplified
version
of
the
ad
trait
in
rust,
the
actual
ones
more
complex
and
rather
than
last
I,
give
this
talk.
It
was
too
confusing
and
we
spent
too
much
time
on
it.
A
So
here's
the
version
of
our
of
our
toy
function.
It's
what
I
get
for
updating
my
slides
by
the
last
minute.
This
says:
I'm
function,
add
I'm
generic
over
a
type
T,
which
is
constrained
by
the
trait
add
I
will
take
a
and
B
both
of
type
T
and
I
will
return
some
types
and
that's
the
complexity
of
the
add
trait
that
I
try
to
remove
my
slides,
I'm,
sorry,
and
so
now.
This
works
with
anything
that
can
be
added
together,
like
a
float.
A
B
A
B
A
Yes,
yeah
so
or
you
might
have
something
like
if
you
multiply
a
float
by
an
integer,
you
get
a
float
okay.
So
the
next
time
to
talk
about
is
custom
types,
we're
not
limited
by
the
types
that
are
in
Russ
to
begin
with,
we
can
make
our
own.
So
this
is
a
very
simple
type:
we're
going
to
be
using
complex
numbers
and
if
you
don't
understand
complex
numbers,
Sok
they're,
just
a
pair
of
numbers.
For
now.
That's
all
we
need
to
worry
about.
A
A
It's
also
usually
a
little
easier
to
type
like
with
the
keyboard,
not
what's
the
type
system,
but
the
new
method
name
is
not
special.
It's
only
by
like
this
is
what
we
do
by
convention,
so
we
could
have
other
constructors,
for
example.
Here
is
a
zero
constructor
for
complex
numbers.
It
takes
no
parameters
and
it
returns
these,
like
the
zero
complex
number,
there's
actually
a
trait
in
the
things
in
the
standard
library,
the
that
is
zero,
but
we
are
going
to
use
it
to
that.
A
A
So
we
have
our
original
and
Bofur
complex
and
then
we
have
another
impulse
or
it
we
say
I'm
going
to
implement
the
trait
ad
for
the
type
complex
and
then
we
assign
the
method
that's
required
by
the
trait
that
raised
it.
We
saw
earlier,
we
define,
that's
that
add
the
takes
of
yourself
and
another
complex
number
and
returns
a
complex
number.
A
So
then,
after
we've
defined
this
implementation
for
complex,
we
can
make
two
complex
numbers
and
we
can
give
them
to
our
toys
function.
It
doubles
the
first
one
that
returns
a
second,
and
this
is
actually
like
coming
from
from
some
other
languages.
This
is
pretty
profound
in
Python.
If
you
try
and
add
two
things
together,
it
might
work.
B
A
Rus,
you
will
know
at
compile
time
whether
this
will
work
in
C++.
If
you
have
templates
I
believe
they're
called
I'm,
not
a
cost-plus
programmer,
but
if
you
trying
to
add
to
two
things
that
can
be
added
together,
you
get
nasty
error
messages.
You
get
miles
of
miles,
long
things
that
say
this
doesn't
work.
It
rescued
a
nice,
simple
error,
message
pointing
exactly
where
you
need
to
look,
and,
unlike,
for
example,
go
we
had
to
opt
in
to
this
behavior
we
had
to
say:
I
can
be
added
this
way.
A
A
We
can
implement
rates
the
same
way.
We
can
make
a
circle
structure
as
a
radio,
and
then
we
can
teach
the
circles
how
to
find
their
area.
We
can
implement
traits
multiple
times.
We
can
have
another
square,
and
now
we
can
treat
these
both
as
shapes
because
we've
implemented
the
shape
trade
for
both
of
them.
A
Everything
examples
how
we
can
use
them
with
this
right,
so
we
have
a
method,
big
enough,
that
is
generic
over
a
type
T,
which
is
constrained
to
be
a
shape
of
an
enclosure
to
the
reference
to
a
shape,
and
if
it's
area
is
more
than
100,
then
it
is
big
enough.
Once
we
design
a
circle
with
radius,
10
and
a
square
for
the
side
length
5,
the
circle
is
big
enough
and
the
square
is
not.
A
A
I
didn't
cover
this.
The
question
is:
why
is
it
generic
overtype
t-shape,
instead
of
just
enclosure,
is
a
reference
to
a
shape,
and
this
is
because
traits
are
not
great.
Don't
have
a
pattern
in
memory,
you
can't
like
say
this
is
a
trait
and
know
exactly
how
big
it
is
exactly
how
it
looks
because
traits
define
behavior,
not
memory.
A
A
A
Okay,
so
one
problem
in
other
languages,
with
this
kind
of
behavior,
is
that
you
might
say
I
want
to
implement
the
add
trait
for
I,
32s
and
redefined
behavior,
and
that
brings
everyone
else
so
kind
of
like
this
I
want
to
define,
add
to
ignore
the
right
hand,
side
double
the
left
hand
side.
This
would
be
a
very
bad
thing
if
you
could
actually
do
it
in
language.
So
you
can't.
This
is
the
kind
of
error
message.
You'll
get
there's
two
different
errors
here.
A
A
Is
that
there's
already
one
in
existence?
The
first
error
is:
what's
hold
a
trait
coherence
problem
and
that's
that
you
do
not
own
the
trait
that
you
try
to
implement
and
you
do
not
implement
you
do
not
own
the
type
you're
trying
to
implement
it
for
so.
In
other
words,
none
of
these
things
are
yours.
You
can't
modify
them.
The
actual
rules
are
more
complex
than
that,
but
that's
a
pretty
basic
under
that's
a
pretty
good
understand.
It'll,
get
you
90%
of
the
way
there.
B
B
A
B
A
A
A
B
A
If
I
remember
my
history
correctly,
the
way
I'm
presenting
it
is
pretty
close
to
the
original
rules
and
then
they
weren't
they
weren't
loose
enough.
So
there
was
a
very
complicated
rewrite
that
said:
okay,
now
we
think
we
can
do
everything
you
want
safely
and
it
wasn't
good
enough
and
there
was
another
even
more
complicated
rewrite
that
made
the
rules
even
a
little
more
tricky
and
the
good
news
is
you
don't
have
to
memorize
these,
you
just
try
it
and
if
it
doesn't
work
the
compiler
with
hell,
you
know
stop.
A
A
So
I
want
to
go
over
some
types
that
are
not
the
basic
types
but
they're
defined
in
the
standard
library
is
a
part
of
the
language
and
there
you'll
find
them
just
about
everywhere.
It's
going
to
be
option,
result
that
can
hashmap
and
we'll
go
over
each
one
of
these
start
with
option
option
is
either
something
or
nothing.
A
That
sounds
really
simple,
but
it
turns
out
to
be
very
useful
to
have
in
Python
or
JavaScript.
You
might
have
a
function
that
returns
the
value
or
undefined
or
none
in
Java.
You
might
have
its
return,
something
or
nulls,
and
this,
if
you
aren't
expecting
it,
is
obtained
it's
terrible
and
if
you
are
expecting
it
is
still
kind
of
a
pain,
I'm,
so
rough,
big
sis,
into
the
type
system
that
make
you
deal
with
it.
You
want
to
return
something
or
nothing.
You
return
an
option
for
something.
A
So
here
we
have
a
method
that
says
I'm
going
to
get
you
directions.
I
want
a
reference
to
a
start,
location
and
reference
to
an
end
location
that
will
give
you
maybe
a
path
I
might
fail.
There
might
not
be
any
path
between
these
two,
so
I
might
say
none,
and
then
this
interacts
with
some.
You
can
see
like
match
on
this,
so
you
say,
I
would
like
to
know
in
this
case.
It's
in
some
I
will
do
this
and
it
is
none
I'll
do
this.
A
This
is
where
the
flow
control
that
I
talk
about
comes
in
similar
to
option
is
result.
A
result
is
either
something
or
an
error.
This
is
often
used
when
you're
dealing
with
I,
oh
yeah,
reading
files
or
document
the
Internet.
Sometimes
things
fail,
and
this
is
distinct
from
option
in
that
option.
The
none
is
not
an
error
case
means
I
have
no
answer.
To
give
you
a
result.
It
is
something
or
an
error,
and
usually
errors
have
messages
attached
to
them,
but
not
always
result
is
marek
over
key.
The
value
and
AD
error.
B
B
A
A
A
We
have
a
mapping
of
people
to
their
offices.
Rather
sorry,
office
numbers
to
people's
names
will
insert
in
office
one
two
three.
We
have
Jane
Smith
in
office
number
196.
We
have
Sarah
Johnson.
So
if
we
try
and
print
the
we'll
get
something
from
office
196,
it
will
say
Sarah
donphan.
If
we
try
and
get
something
for
office
999,
which
is
not
defined.
A
Your
program
will
crash,
so
get
a
panic
and
just
won't
go
any
further.
This
is
because
the
index
rate
doesn't
return
an
option.
It
return,
something
always
something
or
your
program.
Crashes
hashmaps
also
have
a
method
get',
which
does
return
an
option
and
if
you
try
and
get
999
from
this
with
the
method,
you'll
get
an
option.
None
I,
don't
have
anything
like
this
and
if
you
were
to
say
get
for
one
two:
three,
you
get
a
option
that
says
sum
and
then
jane
smith.
B
A
So
this
is
this
is
implementing
from-from
it
into
our
way.
You
deal
with
a
lot
of
conversions
that
are
not
trivial,
so
it's
it's
expensive
to
do
or
any
hand
manipulation
of
memory,
and
usually
you
do.
A
You
take
a
message
that
is
generic
over
some
type,
that
is
in
too
complex,
and
then,
when
you
call
that
method,
you
don't
have
to
necessarily
give
it
a
complex
number.
You
give
it
anything
that
can
change
into
it,
and
here
you
can
see
how
into
and
from
work
together,
because
we've
defined
from
a
table
by
three
twos
to
complex
this
method
works.
A
A
B
A
From
an
end
to
have
this
relationship-
and
that's
documented,
it
doesn't
happen
for
other
trades
unless,
like
those
trees,
have
defined
those
implementations.
B
A
Because
I
didn't
cover
this,
but
you
can
implement
trade
for
some
generic
kind
of
type,
so
you
can
blend
it
for
many
different
types.
A
A
A
That's
a
good
question
from
is
generally
much
more
much
easier
to
implement
than
into
the
traits
a
little
nicer,
but
when
you're
taking
parameters
like
this,
you
want
to
use
into
because
the
API
is
much
nicer
and
they
could
have
done
this
in
one
trait.
It
could
have
had
like
a
from
into
trait,
but
then
they
can't
do
that
trick
where
they
provide
a
blanket
implementation.
A
B
A
A
A
I,
yes,
actually
I'm
I
have
an
example
as
soon
sort
of
it's
a
the
from
string
thing.
In
particular,
it's
actually
really
powerful
for
my
favorite
things
with
trades,
so
one
there's
one
type
of
from
inches
relationship.
That's
really
really
common!
That's
strings!
It's
like
going
from
a
string
to
some
type
or
from
type
to
some
string,
and
these
are
so
common.
There
are
specific
trades
just
to
this.
A
A
Well,
as
NASA
does,
is
it
will
get
it'll
make
a
buffer
which
is
a
string.
We
can
fill
it
data.
It
will
read
from
standard
in
from
the
command
line.
It
will
read
eight
it
into
that
buffer
one-line.
That
unwrapped
is
because
reading
from
standard
in
might
sail
various
reasons.
If
it
does
fail,
this
program
would
crash.
You
can
be
a
little
careful
and
say:
oh,
there
was
an
error.
L
will
return
something
else,
but
that
doesn't
fit
on
the
slide.
A
No
parse
is
part
of
the
from
string
that
so
it
take
anything,
takes
a
string
return,
something
that
is
from
string
and
then
unwrap
site
to
that
might
sail
to
and
then
down
below.
We
call
this
twice.
I
would
like
to
read
an
I-32
I'd
like
to
read
an
f32,
and
just
like
you
said
earlier,
it
will
figure
out
what
you
wanted
to
return
based
on
what's
happening.
A
B
A
A
Okay,
so
I
mentioned
it
feel
like
from
and
into
our
pair
of
hair
and
from
store
lets.
You
go
from
strings
to
some
object.
We
don't
have
the
other
side
of
that
pair.
One
instance
of
that
other
side
are
the
debugging
display
traits
anytime.
You
try
and
print
something
you're,
either
probably
going
to
be
interacting
with
either
debug
or
display.
If
you
use
just
two
curly
braces,
that's
display,
that's
meant
for
like
pretty
human
output
and
if
you
use
a
quest
or
colon
question
mark,
that
is
the
debug
version
yeah.
A
And
this
is
the
the
traits
very
simplified
version
of
the
scrapes
debugging
displays
are
similar.
They
both
have
a
method
called
format
which
takes
some
reference
to
some
object,
and
then
they
give
it
a
mutable
formatter
function,
and
implementers
of
this
trait
are
expected
to
you
to
call
methods
on
the
formatter
to
write
things
out
to
whatever
string
you're
doing
dealing
with
I
might
be
a
file,
it
might
be
string,
it
might
be
standard
out
a
lot
of
options.
A
So
if
you
on
implement
display,
for
example,
for
our
complex
numbers
now,
let's
just
step
of
that
type
forgot-
that's
fine,
but
you
call
various
methods
on
format
to
write
to
that
string
and
you
write
your
different
variables.
If
you
want
to
implement
debug,
you
can
do
it
for
it.
You
can
do
it
automatically.
If
you
say
I
would
like
a
imitation
of
debug.
The
compiler
knows
how
to
do
that
old
derivation
and
for
structs.
A
A
Basically,
every
struct
could
probably
implement
debug
unless
for
some
reason
to
camp
and
as
I
can't,
maybe
you
should
think
about
why
I
camp?
This
is
very
useful
for
debugging
error
messages.
This
generally
makes
your
life
better
implanting
display
is
something
that
not
everything
needs
to
do,
but
if
you,
if
you
want
to
write
so
that's
qyx
mayline
our
interface
is
that's
pretty
useful
and
turn.
If
no,
please.
A
B
A
One
of
them
are
show
leaked
actually,
and
you
can
also
do
Ord
and
I
fully.
So
I
am
I'm,
not
sure
sighs.
Maybe
so
all
these
are
traits
we're
talking
about.
So,
if
example,
partial
ich
means
it's
partial
equality
means
you
can
purchase
objects
and
say:
are
they
equal
or
not,
and
sometimes
I
might
say,
I
don't
know,
because
floating-point
numbers.
B
A
We
were
talk
about
this
earlier.
The
the
foot,
the
floating
point
standard
requires
that
there
is
a
value
called,
not
a
number
which
it's
not
equal
to
itself
is
not
greater
than
itself,
not
less
than
it's
all
stated.
It's
just
not
anything
to
anything.
So
that's
why
we
have
Marshall
League
and
you
look
at
all
the
all
the
types
in
the
standard
library,
the
only
one
that
doesn't
implement
eat
this
whole
equality
is
floating
points,
so
I,
Triple
E
did
this
to
us.