►
From YouTube: Shared borrows
Description
No description was provided for this meeting.
If this is YOUR meeting, an easy way to fix this is to add a description to your video, wherever mtngs.io found it (probably YouTube).
A
Oh
welcome
to
the
second
tutorial.
The
topic
we're
going
to
talk
about
today
is
sharing
continuing
our
theme
of
everything.
I
need
to
know
about
rust,
I
learned
in
kindergarten
right
and
in
particular.
What
we
want
to
do
is
address
this
problem
that
we
hit
at
the
end
of
the
last
tutorial
at
the.
If
you
recall,
we
had
this
shared
buffer
or
the
string
buffer,
that
we
called
name,
and
we
were
trying
to
call
a
function
twice
and
the
first
time
we
call
it.
A
We
gave
ownership
of
our
buffer,
but
then
we
were
in
trouble
because
we
didn't
have
ownership
anymore
when
we
wanted
to
call
it.
The
second
time-
and
one
solution
we
saw
is
that
we
could
clone
this
buffer
the
first
time,
but
that
seems
a
little
bit
expensive.
We're
copying
the
string.
What
you
don't
really
need
to
do
so
we're
going
to
learn
today
is
much
more
I
would
say,
is
a
much
more
idiomatic
solution
here,
which
is
to
share
the
string
between
the
two
functions.
A
Now,
if
we
go
back
to
our
kind
of
real
life
analogy
right,
this
is
actually
again
a
pretty
normal
thing
right.
So
if
I
have
a
book
and
I
hand
it
to
you,
it
might
be
that
I'm
giving
it
to
you
as
a
present
forever,
but
usually
what's
happening.
Is
that
I'm
just
saying
why
don't
you
read
this
book
I
think
you
might
like
it
and
when
you
finished
with
it,
you
give
it
back
to
me
right
so
kind.
A
If
I
say
here,
you
go
principles
of
compiler
to
then
check
out,
chapter,
13
or
I,
don't
even
know.
If
there
are
13
chapters
check
out
chapter
2,
it's
really
good,
then
you
say
I'm
very
interesting,
I
liked
it
and
you
give
it
back.
That's
how
data
structures
work
in
rust,
too.
Right
is
a
very
common
scenario
that
we
want
to
say:
here's
a
string
I'd
like
you
to
print
it
on
the
screen
and
when
you're
finished
using
it
I
might
do
other
things
with
it.
A
Okay,
so
here
we
see
a
similar
code
snippet
to
the
one
we
saw
in
the
ownership
tutorial,
but
this
time
we're
going
to
use
borrowing
to
avoid
copying
the
data
all
right,
so
we're
going
to
call
helper
twice,
but
we're
not
going
to
copy
the
data.
The
second
time,
how
does
it
work?
It
starts
off
just
the
same,
that
I
say:
let
name
equals
format,
bang
and
that's
going
to
allocate
me
a
string
buffer
represented
by
this
book.
But
what
happens
next
is
different.
A
What
I'm
doing
here
with
this
ampersand
name
is
I'm,
actually
borrowing
the
variable
name
to
create
a
reference
which
is
stored
in
the
variable
R,
okay
and
I'm
symbolizing
that
by
shrinking
the
book.
So
our
now
is
a
reference
to
this
string
name
and
the
variable
name
actually
owns
the
string
and
when
I
call
helper,
you
see
that
helper
no
longer
expects
to
take
ownership
of
a
string.
Instead,
it's
it's
argument,
type
is
ampersand
string.
A
That
means
helper
is
saying,
give
me
a
reference
to
a
string
called
name
right
and
this
string
could
be
owned
by
any
of
any
either
either
by
Maine
or
by
somewhere
else
of
the
stack.
So
that
means
that
when
helper
runs,
it's
just
going
to
have
this
reference,
pointing
to
the
name
which
is
owned
by
Maine
right.
So
now
a
helper
will
execute.
It'll
read
this
string
through
the
reference
and
when
helper
returns,
no
memory
gets
freed
here,
because
helper
didn't
own.
A
The
string
helper
just
had
a
reference
to
this
string,
so
I
was
just
borrowing
this
string,
and
now
we
get
back
here.
You
see
we
can
call
helper
again
and
we
can
give
the
same
shared
reference
right
and
that's
no
problem.
That's
because
shared
references
like
are
are
actually
copy
types.
They
can
be
used
as
many
times
as
you
want.
Essentially,
once
you
started
sharing
access
to
a
string
you
can
and
you
can,
then
you
can
make
any
number
of
references
to
that
same
string
right
now.
A
Eventually
we're
going
to
finish
after
this
at
this
point
when
we
reach
the
brace
now
this
reference
R
goes
out
of
scope,
which
means
that
we
stopped
borrowing
string
because
we're
not
using
this
reference
anymore
and
now.
At
this
point,
when
Maine
returns,
all
the
data
the
Maine
owns
is
going
to
get
freed,
which
includes
the
string
because,
basically,
here,
instead
of
whereas
before
we
gave
ownership
of
the
string
to
helper
and
then
helper
freed
it
this
time
we
kept
ownership
within
the
main
function,
the
whole
time.
A
Now
the
references
are
really
cool
because
they
let
you
share
data
and
have
lots
of
lots
of
different
earth,
share
it
references
to
the
same
data.
However,
there
is
a
kind
of
catch
all
the
while
this
data
is
shared
and
borrowed.
It
is
also,
generally
speaking,
immutable.
So
if
you
have
a
lot
of
people
reading
from
the
same
data
structure,
that
means
that
none
of
them
writing
to
it
right
so,
for
example,
helper
function.
This
is
the
helper
function.
A
We
had
all
that
it
did
was
print
to
the
screen
and
that's
okay,
because
it's
just
reading
from
the
string
perfectly
good.
However,
if
the
helper
function
we're
trying
to
call
this
push
method,
you
might
recall,
we
saw
that
last
time
push
adds
a
character
onto
a
string
growing
the
buffer
at
the
mutation.
This
would
actually
get
a
compilation
error.
We
would
see
something
like
you
can't
borrow
a
mutable
borrowed
content
now
I'm,
actually
simplifying
here
shared
data
is
not
always
immutable.
A
What
it
is
is
that's
the
default
and
if
you
it's
kind
of
the
best
way
to
handle
it,
there
are
types
which
enable
mutation
in
shared
data,
but
those
types
have
to
go
through
have
to
have
some
kind
of
API
that
makes
it
safe
to
do
so.
So
here's
an
example,
a
mutex
or
a
lock
that
that
would
allow
you
to
share
data
between
threads,
but
it
wouldn't
be
safe
for
all
of
those
threads
to
go
mutating
or
reading
from
that
data.
A
A
Now
what
I'd
like
to
do
is
take
these
examples
and
I'm
going
to
go
over
to
the
Play
website
and
just
do
some
interactive
code,
editing
that
will
give
you
a
feeling
for
for
what
I'm
talking
about
alright.
So
here
we
are
at
play,
and
this
is
the
example
we
ended
with
our
ownership
tutorial
with.
If
you
recall,
we
have
a
name
and
we're
trying
to
call
helper
twice
and
we're
getting
this
error
use
of
move
value
name.
A
The
reason
is
we're
giving
ownership
of
the
name
here
in
the
first
line,
as
it
says
when
your
value
moved
here,
but
then,
when
we
try
to
access
it
again,
we've
already
given
ownership
away,
so
we're
trying
to
use
something.
We
don't
have
anymore
and
that's
not
allowed.
So
we
said
we
can
solve
this
by
using
borrowing
in
particular,
shared
borrows
unshared
references
all
right.
So
here
when
I
make
the
first
step
is
I
have
to
change
helper
so
that,
instead
of
saying
I
need
to
own
a
string.
A
Helper
just
says:
I
need
to
borrow
a
string
from
you
for
you
to
call
me
and
in
terms
of
rust,
what
that
means
is,
instead
of
taking
a
string
as
argument
we're
going
to
take
a
reference
to
a
string,
but
if
I
just
compile
this
you'll
see
I
get
errors
over
here
at
the
call
sites
saying
mismatched
types,
the
idea
is
both
the
caller
and
the
callee.
You
have
to
agree
about
who
owns
what
and
who's
borrowing
what
right
so
right
here
when
we
say
helper
name
but
main,
is
sort
of
trying
to
give
ownership.
A
The
helper
and
helper
doesn't
want
to
take
it.
That's
an
intuitive
way
to
look
at
it.
So
we
have
to
change
the
lend
out,
name
right
and
now
main
is
creating
a
reference
and
giving
the
reference
to
helper
a
helper
is
happy
to
accept
the
reference
into
its
job,
and
you
see
that
now
our
code
compiles
because
we
can
lend
out
the
same
value
multiple
times
and
we
retain
ownership
the
entire
time
now.
A
What
we
saw
is
that
this
ant
name
is
really
just
an
expression
right,
so
we
might
make
a
variable
like
so
and
now
I'm
creating
a
reference
on
borrowing
name,
making
a
reference
and
storing
it
in
our
and
I
can
use
this
R
as
many
times
as
I
want
all
right.
So
the
type
of
this
is
the
ampersand
string
right
and
a
reference
is
really
just
another
value
that
can
be
passed
around
and,
as
we
saw
at
the
end,
there
are
some
values
that
are
so
cheap.
They
can
be
copied.
A
Well,
a
shared
reference
like
this
one
or
just
a
reference,
as
I
usually
say,
can
be
passed
around
it's
a
copy
type
right,
so
you
can
create
as
many
copies
as
you
like,
and
that's
just
fine.
Okay.
So
that's
the
basic
idea
of
borrowing
but
I
want
to
explore
more
deeply
the
matter
of
mutability.
You
may
recall:
I
said
that
shared
references
while
the
reference
exists.
The
data
has
to
be
immutable,
or
at
least
is
normally
immutable.
A
Unless
you
have
a
lock
or
some
other
means
to
protect
access
right,
and
you
might
have
thought
that
means
that
the
following
code
would
not
compile
so
I've
just
modified
to
declare
the
variable
name
to
be
mutable
like
we
saw
an
ownership
tutorial
and
I'm
going
to
push
a
new
letter
on
it
right.
So
when
I
run
now
you
might
expect
is
not
to
compile.
A
But
it
does
you
see
we're
now:
printing
West
stations,
X
the
reason
you
might
have
expected
not
to
compile
as
I
said,
that
shared
references
are
immutable,
and
yet
this
value
is
clearly
mutable
right
and
the
catch.
Is
that
the
way
rust
views
mutability?
Is
it's
a
very
time-based
thing?
Nothing
is
purely
mutable
or
immutable.
It's
a
question
of
when
is
it
mutable?
A
Now
you
can't
go
ahead
and
mutate,
and
so
that
applies
until
the
variable
R
goes
out
of
scope
right.
So
it's
just
as
true
here
I'm
going
to
get
an
error,
and
it's
also
true
here
after
both
of
the
helper
calls
have
finished
now.
Sometimes
people
find
this
a
little
surprising
because
they
think
well
I'm
done
using
the
variable
R.
Why
is
am
I
still
prevented
from
modifying
name?
Isn't
the
borrow
over
the
well?
And
the
answer
is
that
it's
not
at
least
not
the
way
the
compiler
works
today.
A
The
borrow
extends
until
that
variable
R
actually
goes
out
of
scope
all
right.
So
if
I,
for
example,
create
a
new
scope,
so
you
can
insert
blocks
wherever
you
like
right
and
now
this
variable
R
it's
it's
constrained
to
only
be
used
inside
this
block,
so
I
can't
name
R
here.
This
would
be
an
out
of
scope.
Reference
right.
This
variable
doesn't
exist,
but
now
the
borrow
will
end
here
at
the
closing
block
and
hence
I
can
run
this
code
and
it
compiles
just
fine.
A
A
So
we're
almost
done
here
with
the
shared
borrows,
but
I
want
to
show
you
one
really
cool
thing
that
follows
from
them
in
rust,
so
we've
in
all
the
examples
we've
looked
at,
we've
had
a
shared
reference
to
an
entire
string
buffer,
but
a
lot
of
times
you
just
want
to
process
a
subset
of
a
string
like
the
first
word
or
something
like
that.
Right
and
rust
makes
that
really
easy
and
really
efficient
and
to
see
how
it
works.
We
have
to
dig
a
little
bit
deeper
into
what
strings
look
like
at
runtime.
A
So
when
I
make
a
string
buffer
like
on
this
highlighted
line
of
code,
where
I
say,
let
name
equals
format,
bang
it
has
this
shape
at
run
time.
A
string
buffer
is
really
three
bits
of
data
there's
the
data
which
is
a
kind
of
reference
to
the
buffer
itself
in
memory
right.
So
in
that
case
they
might
have
the
word
was
station
in
it
and
then
there's
a
length
and
a
capacity.
The
length
is
the
amount
of
data
that
we're
actually
using
in
our
buffer,
and
the
capacity
is
the
total
space
available.
A
Usually
we
have
extra
space
so
that
when
we
append
things
to
the
string,
we
have
some
place
to
put
them
right,
and
now
here
I'm
going
to
call
helper,
you
see
that
the
helper
function
looks
a
little
different.
Instead
of
taking
an
ampersand
string
with
a
capital
S,
it
takes
an
ampersand
sterve,
the
lowercase
s,
that's
called
a
string
slice,
so
the
type
ster
and
rust
means
basically
some
subset
of
a
string
buffer
right
and
that's.
A
He
always
were
almost
always
something
you
borrow
from
from
from
a
string
buffer
and
on
the
other
side
we
didn't
just
write:
ampersand
name,
we
wrote
ampersand
name
and
we
had
these
brackets
and
said
from
the
character
1
on.
So
that
means
is
this
going
to
skip
the
first
character
in
the
string?
Actually
the
right.
A
So
in
this
case,
when
we're
going
to
create
a
reference,
what
we
wind
up
doing
is
we
have
a
reference
directly
into
the
string
buffer
just
skipping
over
the
first
letter.
What's
really
cool
about
this?
Is
that
there's
no
allocation
and
no
copying
right,
we're
just
kind
of
keeping
our
finger
into
the
data
that
is
in
the
big
string
buffer
and
just
some
subset
of
that
data,
and
you
see
there's
also
a
length.
This
tells
us
how
many
characters
we
have
left.
A
So
the
length
of
this
substring
would
be
one
less
than
the
length
of
the
full
string,
and
now
this
is
just
a
borrowed
reference
like
anything
else,
it's
just.
Instead
of
referring
to
the
whole
string
buffer.
It
refers
to
a
subset
of
its
data,
so
we
can
call
helper
and
when
helpers
done,
no
memory
is
going
to
get
freed
and
you
see
that
we
can
also
pass
helper.
A
The
whole
string,
just
like
we
did
before
ampersand
name
without
any
of
that
sub
setting
stuff,
and
it
still
works
just
fine
because
of
course,
a
subset
of
the
whole
string
is
also
a
subset
of
itself
right.
So
this
looks
a
lot
like
the
syntax.
You
might
see
in
other
languages,
you
see
in
the
bottom.
You
can
have
Python
as
similar
syntax
in
Ruby,
but
unlike
those
languages,
we're
not
making
a
new
string
object
and
returning
it
we're
just
having
a
kind
of
shared
reference
to
a
subset
of
the
strings
data.
A
This
really
scales
up
when
you
start
building
higher
level
abstractions
that
work
over
strings
right.
So
here,
for
example,
we
see
a
snippet
of
code
that
iterates
over
a
line
and
sums
up
the
length
of
each
word
in
the
line,
so
it
kind
of
skips
whitespace.
So
this
line
dot
split.
This
is
actually
a
method
on
strings
string
slices,
actually
the
returns,
an
iterator
that
will
yield
up
the
whitespace
separated
in
this
case,
white
space,
separated
sub
strings,
and
so
basically,
it's
going
to
return
an
iterator
over
all
the
words.
A
So
if
we
have,
this
is
our
line,
which
is
the
full
string
buffer,
saying
goddess.
If
Achilles
is
raged,
black
and
murderous,
then
each
time
we
pull
something
out
of
the
iterator
we're
going
to
get
a
new
sub
slice.
So
first
we'll
get
sing,
then
goddess
and
of
and
so
forth
right,
and
because
each
of
these
can
be
constructed
very
cheaply
when
the
optimizer
is
finished.
A
With
this
loop,
we're
going
to
get
something
just
like
we
would
have
written
in
C,
which
is
basically
a
pointer
pointing
into
this
buff
memory
and
bumping
ahead
by
a
couple
bites
each
time
very,
cheap,
very
different,
and
if
we
had
this
kind
of
high-level
code
in
some
other
language,
where
you
would
actually
have
to
allocate
objects
for
the
iterator
allocate
objects
for
the
slices
of
the
strings
that
you're
returning
and
so
forth.
And
so
this
is
why
rust
is
able
to
have
this
high
level
code
with
such
low
level
performance.
A
That
brings
us
to
the
end
of
our
tutorial.
So
the
last
thing
is
left
is
the
exercise.
Now,
if
you
look
down
below
this
YouTube
video
here
on
the
website
or
the
URL
that
you
see
in
the
corner,
there
then
you'll
find
the
shared
borrows
exercise.
So
I
want
you
to
click
on
it
and
we'll
go
over
it
real
briefly,
and
you
should
do
the
goals
and
then
come
back
here
for
the
solution.
Okay,
so
here's
the
exercise.
A
This
is
basically
our
traditional
example
of
calling
greet
twice
and
what
we're
going
to
do
now.
The
first
goal
is
to
convert
this
function
so
that
greet
instead
of
taking
ownership
of
this
string
borrows
the
string
and
therefore
we
can
remove
this
call
to
clone
right.
If
we
just
do
that
now,
of
course,
we're
going
to
get
a
compilation
error
because
we're
using
the
moved
value
twice
and
second
goal
once
you've
done,
that
is
to
insert
some
sub
slicing
so
that
we
can
print
hello
friend
instead
of
hello,
my
friend
alright
get
to
it.
A
Doo
doo
doo,
doo
doo,
two,
two,
two
two,
you
know
I'm
gonna
have
to
find
the
new
songs,
it's
getting
kind
of
old
okay.
So
this
was
the
function
we
had
to
start
and
if
we
run
we're
going
to
get
compilation
errors,
we
first
want
to
change
it
to
use
borrowing.
So,
in
that
case,
we
have
to
change
instead
of
taking
ownership
of
the
string,
we
think
of
shared
reference,
and
similarly,
we
supply
not
the
string
itself,
but
a
shared
reference
to
the
string
and
now
the
code
compiles.
A
Now
we
wanted
to
use
a
sub
slice
to
skip
the
first
few
letters.
So
one
way
we
could
do
that
is
to
skip
it
right
here
at
the
point
of
printing.
What
is
it
so
there's
this
problem
with
the
play
server
I'm
going
to
leave
this
in
the
screencast
just
because
it's
occasionally
worth
knowing
about
where
sometimes
it
will
timeout,
because
it
would
happen
to
be
heavily
in
use
at
the
time
and
then
it
will
cache
that
timeout.
We
should
fix
that
bug.
A
But
one
thing
you
can
do
is
just
modify
your
program
in
some
way
to
bypass
the
cache
anyway.
Here
you
see
hello
friend,
so
we
skipped
the
first
three
letters,
that's
what
we
wanted
to
do,
but
it
seems
a
little
weird
to
have
greet
skipping
the
first
three
letters,
because
that
implies
that
there
will
always
be
like
for
every
name.
A
Now,
if
we
do
this
now,
we'll
see
hello
friend
the
first
time
and
hello,
my
friend
the
second
time.
Okay,
thank
you
very
much
for
attending
this
shared
Barros,
tutorial,
I
hope
you
will
enjoy
future
tutorials,
one
that
might
be
so
to
directly
carry
on
with
this
work,
there's
a
tutorial
on
mutable
borrowing.
That
shows
basically
how
you
can
make
references
like
this,
but
you
can
actually
mutate
the
thing
they
refer
to
instead
of
having
immutable
access,
and
then
there
are
other
tutorials
covering
other
aspects
of
rust.
Thanks
a
lot.