Return to Video

Josh Matthews - An Introduction To Rust

  • Nesincronizat
    (technical issues/chatter)
  • Nesincronizat
    (People are not important.)
  • Nesincronizat
    (There are no peole in technology,
    just replaceable components.)
  • Nesincronizat
    (Laughter)
  • Nesincronizat
    6:18 Alright let's do this
  • Nesincronizat
    I'm going to tell you about what Rust is
  • Nesincronizat
    Why Rust is a part of Mozilla's work
  • Nesincronizat
    and when it's going to start
    being a part of Mozilla's work.
  • Nesincronizat
    Let's begin
  • Nesincronizat
    so Rust is a new programming language
  • Nesincronizat
    that has been created inside Mozilla
  • Nesincronizat
    and shared with the outside world
  • Nesincronizat
    The goal of it
  • Nesincronizat
    is basically
  • Nesincronizat
    anytime when you traditionally
  • Nesincronizat
    as a developer
  • Nesincronizat
    have reached for C++
  • Nesincronizat
    because you need to have certain
    guarantees
  • Nesincronizat
    about performance for example
  • Nesincronizat
    we want Rust to be a viable alternative
  • Nesincronizat
    in every single case
  • Nesincronizat
    There should be no reason to choose C++
  • Nesincronizat
    over Rust
  • Nesincronizat
    in the ideal future
  • Nesincronizat
    So it began as a side project
  • Nesincronizat
    of a Mozillian named Graydon Hoare
  • Nesincronizat
    before 2009
  • Nesincronizat
    he began working on it full-time
  • Nesincronizat
    in 2009
  • Nesincronizat
    because Mozilla saw the ideas that he had
  • Nesincronizat
    and said we can see
    how we could put this into Firefox
  • Nesincronizat
    and make the product better
  • Nesincronizat
    if we actually had this language
  • Nesincronizat
    so that's when we started investing
    time and resources into it
  • Nesincronizat
    fast forward a few years
  • Nesincronizat
    we now have a team under
    the Mozilla Research umbrella
  • Nesincronizat
    whose working full-time
    on improving the language
  • Nesincronizat
    and we released version 1.0
    last year
  • Nesincronizat
    and every 6 weeks we
    now release a new version
  • Nesincronizat
    so we just released 1.13
  • Nesincronizat
    and there is a large community
  • Nesincronizat
    around the language now
  • Nesincronizat
    so not only do we have developers
  • Nesincronizat
    working in Mozilla on it
  • Nesincronizat
    some of those developers
  • Nesincronizat
    are part of what is called the Core Team
  • Nesincronizat
    and those are the people that decide
    the future of the language
  • Nesincronizat
    and help shape where it's going
    as time goes on
  • Nesincronizat
    so in addition to our employees
  • Nesincronizat
    we have volunteers
  • Nesincronizat
    and employees from other companies
  • Nesincronizat
    that are now depending on Rust
    for other products
  • Nesincronizat
    and they're helping shape this ecosystem
  • Nesincronizat
    which then has a large number of libraries
  • Nesincronizat
    that are being developed by people
  • Nesincronizat
    who are enthusiastic about the language
  • Nesincronizat
    it's got a lot of downloads happening
  • Nesincronizat
    since the last time I gave this talk
  • Nesincronizat
    there've been 10 million more downloads
  • Nesincronizat
    and 300 new libraries published
  • Nesincronizat
    so there's a lot going on
  • Nesincronizat
    there's also the friends of Rust
  • Nesincronizat
    which I'll show you
  • Nesincronizat
    [Web page: Rust reliant Companies]
  • Nesincronizat
    a page showing lots of different companies
  • Nesincronizat
    that've told us
  • Nesincronizat
    "Yes, we are relying on Rust in production,
  • Nesincronizat
    we are very excited about these prospects".
  • Nesincronizat
    so, lots of cool things going on there
  • Nesincronizat
    so the reason for Rust
  • Nesincronizat
    is that we've in the past
  • Nesincronizat
    10 or more years of using Firefox
  • Nesincronizat
    and Javascript in Firefox
  • Nesincronizat
    sorry C++ and Javascript in Firefox
  • Nesincronizat
    we've come across the fact that
  • Nesincronizat
    there are some pervasive problems
  • Nesincronizat
    that are caused by using C++
  • Nesincronizat
    that is
  • Nesincronizat
    writing code that makes use
    of local threads
  • Nesincronizat
    is very difficult
  • Nesincronizat
    it's easy to write incorrect code
  • Nesincronizat
    and it is very easy to write code
  • Nesincronizat
    which is unsafe
  • Nesincronizat
    and that translates into
  • Nesincronizat
    security exploits
  • Nesincronizat
    things that are stored by governments
  • Nesincronizat
    and by hackers
  • Nesincronizat
    all over the world
  • Nesincronizat
    and used to
  • Nesincronizat
    steal data from users
  • Nesincronizat
    and take over their computers
  • Nesincronizat
    using the browser as a vector
  • Nesincronizat
    there's a huge number of these
    that are released every year
  • Nesincronizat
    by security investigators
  • Nesincronizat
    and there's competitions
  • Nesincronizat
    showing who can pwn a browser the quickest
  • Nesincronizat
    so we looked for ways to address this
  • Nesincronizat
    by creating a new language
  • Nesincronizat
    and it turns out that actually
  • Nesincronizat
    both of these issues
  • Nesincronizat
    make it difficult to write parallel code
  • Nesincronizat
    that makes use of modern hardware
  • Nesincronizat
    and writing code that is safe
  • Nesincronizat
    and not exploitable
  • Nesincronizat
    turns out they can both be addressed
  • Nesincronizat
    by introducing the concept of Ownership
  • Nesincronizat
    in the language
  • Nesincronizat
    so let's talk about what that means
  • Nesincronizat
    Ownership encapsulates 2 ideas basically
  • Nesincronizat
    it is what the owner of a value is
  • Nesincronizat
    whether there are multiple
    owners of a value
  • Nesincronizat
    or mulitple users that are sharing a value
  • Nesincronizat
    and additionally when that value
    is actually valid
  • Nesincronizat
    what is the point in the program
  • Nesincronizat
    in which it's actually safe
    to interact with the value
  • Nesincronizat
    and so from this we can derive a few rules
  • Nesincronizat
    that if there is only a single owner
    for a value
  • Nesincronizat
    that value can be destroyed
    when the owner goes out of scope
  • Nesincronizat
    because there are no ways
    to reference that value anymore
  • Nesincronizat
    similarly if a value
    has only a single owner
  • Nesincronizat
    that means that it is legal to mutate it
  • Nesincronizat
    because no one can observe
    the change to that value
  • Nesincronizat
    and finally
  • Nesincronizat
    if you have multiple owners
  • Nesincronizat
    you need to have
  • Nesincronizat
    something called Borrowing
  • Nesincronizat
    which is
  • Nesincronizat
    you still have a single owner
  • Nesincronizat
    but other people can observe it
  • Nesincronizat
    but it cant be changed during that time
  • Nesincronizat
    so let's talk about Analogy here
  • Nesincronizat
    if I've got a colouring book and a marker
  • Nesincronizat
    and some friends
  • Nesincronizat
    we can establish some rule
  • Nesincronizat
    the colouring book and the marker
  • Nesincronizat
    are my property
  • Nesincronizat
    so they go with me wherever I go
  • Nesincronizat
    if I leave no one else can
    observe the book
  • Nesincronizat
    or colour in it
  • Nesincronizat
    however I am able to loan the book
    to someone else
  • Nesincronizat
    and that means
    they're borrowing it from me
  • Nesincronizat
    that also means that I can't colour in it
  • Nesincronizat
    while they're borrowing it
  • Nesincronizat
    because that's rude
  • Nesincronizat
    so they can also choose to lend it
    to people around them
  • Nesincronizat
    but that means that if
    anyone else is borrowing it
  • Nesincronizat
    they need to give it back to me
    before they leave
  • Nesincronizat
    because it's my property
  • Nesincronizat
    and only I can leave with it
  • Nesincronizat
    additionally
  • Nesincronizat
    If I have the marker
  • Nesincronizat
    I'm able to colour in the book
  • Nesincronizat
    if I have the book
  • Nesincronizat
    I can also give the marker to someone else
  • Nesincronizat
    and that means they can colour in the book
  • Nesincronizat
    if they have it
  • Nesincronizat
    that means that I'm no longer able to colour
  • Nesincronizat
    because there's only a single marker
  • Nesincronizat
    and finally if I want to leave
  • Nesincronizat
    but I dont actually have
    the book in my posession
  • Nesincronizat
    there's a problem
  • Nesincronizat
    either I need to wait until
  • Nesincronizat
    someone gives me the book back
  • Nesincronizat
    or I actually need to give it
    to someone else
  • Nesincronizat
    and say OK it's not my property anymore
  • Nesincronizat
    it's yours to deal with
  • Nesincronizat
    so Ownership in Rust
  • Nesincronizat
    if we have a Type we call a ColouringBook
  • Nesincronizat
    and it gives us a constructor
  • Nesincronizat
    that returns a new instance
    of a ColouringBook
  • Nesincronizat
    we can say OK
  • Nesincronizat
    here is a variable called a Book
  • Nesincronizat
    which we will say
  • Nesincronizat
    stores this ColouringBook value
  • Nesincronizat
    that means that we can now call
  • Nesincronizat
    call this colour method
  • Nesincronizat
    which will change the ColouringBook
  • Nesincronizat
    in some way
  • Nesincronizat
    it'll colour in it
  • Nesincronizat
    that's fine
  • Nesincronizat
    we can mutate it
  • Nesincronizat
    because we only have a single owner
  • Nesincronizat
    so what happens if
  • Nesincronizat
    someone borrows the book?
  • Nesincronizat
    we now have another variable
  • Nesincronizat
    called borrowed_book
  • Nesincronizat
    and we use the ampersand
  • Nesincronizat
    to show that we are borrowing the value
  • Nesincronizat
    that's stored in this value called Book
  • Nesincronizat
    this variable called Book
  • Nesincronizat
    this is an Immutable Borrow
  • Nesincronizat
    that measn that it is not possible
  • Nesincronizat
    to mutate the original value anymore
  • Nesincronizat
    because it's borrowed
  • Nesincronizat
    so we cannot colour in the book
  • Nesincronizat
    while it's borrowed by someone else
  • Nesincronizat
    as far as our Analogy
  • Nesincronizat
    the other thing however
  • Nesincronizat
    is that if someone else is
    borrowing the book
  • Nesincronizat
    they also cannot colour in it
  • Nesincronizat
    they don't have the marker
  • Nesincronizat
    in this case they only have
    an Immutable Borrow
  • Nesincronizat
    so they cannot mutate it
  • Nesincronizat
    so we can work around this
  • Nesincronizat
    by using what's called a Mutable Borrow
  • Nesincronizat
    this says OK
  • Nesincronizat
    give me the book
  • Nesincronizat
    and give me the marker
  • Nesincronizat
    and then they have both those elements
  • Nesincronizat
    necessary in order
    to actually colour in it
  • Nesincronizat
    and mutate it in this case
  • Nesincronizat
    so we have the ampersand
  • Nesincronizat
    followed by the Mute keyword
  • Nesincronizat
    and this means
  • Nesincronizat
    give me a mutable borrow
  • Nesincronizat
    to the value that follows
  • Nesincronizat
    however the thing to note here
  • Nesincronizat
    is that we cannot use the original value
  • Nesincronizat
    called Book
  • Nesincronizat
    in order to mutate it while it's borrowed
  • Nesincronizat
    we cannot have 2 different people
  • Nesincronizat
    colouring in it
  • Nesincronizat
    becasue only one person
    can colour at a time
  • Nesincronizat
    so let's go back to the example
    we had before
  • Nesincronizat
    so we've got a Book value
  • Nesincronizat
    we're allowed to colour in it
  • Nesincronizat
    because there's no one borrowing it
  • Nesincronizat
    then on the next line
  • Nesincronizat
    we have an Immutable Borrow
  • Nesincronizat
    and we're not allowed to colour
  • Nesincronizat
    in the original one again
  • Nesincronizat
    this is fine
  • Nesincronizat
    it's borrowed
  • Nesincronizat
    it can't be coloured in
  • Nesincronizat
    so let's fix this
  • Nesincronizat
    this where we talk about Lifetimes
  • Nesincronizat
    remember how I said that
  • Nesincronizat
    we can figure out
    when a value is valid
  • Nesincronizat
    so this means that if we have a scope here
  • Nesincronizat
    deliniated by
  • Nesincronizat
    the curly braces
  • Nesincronizat
    this means that inside the curly braces
  • Nesincronizat
    anything that is unique inside there
  • Nesincronizat
    no longer exists outside of them
  • Nesincronizat
    so if we create this Borrowed Book value
  • Nesincronizat
    and we borrow from the book value
  • Nesincronizat
    we have a Mutable Borrow
  • Nesincronizat
    and that's fine
  • Nesincronizat
    we can then do something with this
  • Nesincronizat
    that doesn't require mutating it
  • Nesincronizat
    we can just look through the book
  • Nesincronizat
    we can investigate it
  • Nesincronizat
    and that doesn't mutate it
  • Nesincronizat
    that's fine
  • Nesincronizat
    and so previously calling Colour
  • Nesincronizat
    on the original Book value
  • Nesincronizat
    would've been an error
  • Nesincronizat
    because it was Borrowed
  • Nesincronizat
    but now
  • Nesincronizat
    the Borrow no longer exists
  • Nesincronizat
    because it is only valid
    within the scope of these braces
  • Nesincronizat
    this is how we can control
  • Nesincronizat
    the scope of a Borrow
  • Nesincronizat
    so then finally
  • Nesincronizat
    we can also transfer Ownership
  • Nesincronizat
    this is how we talk about giving
    away the book to someone else
  • Nesincronizat
    by default we can say OK
  • Nesincronizat
    we'll create a new variable called Book 2
  • Nesincronizat
    and it will have the contents
    of the Book variable
  • Nesincronizat
    and we can now colour in Book 2
  • Nesincronizat
    that's fine
  • Nesincronizat
    it's not a Borrow
  • Nesincronizat
    there's no Ampersand there
  • Nesincronizat
    and we are giving control of the value
  • Nesincronizat
    to another variable
  • Nesincronizat
    and additionally
  • Nesincronizat
    if you try to compile this
  • Nesincronizat
    you'll note that
  • Nesincronizat
    we can no longer interact
    with the Book variable
  • Nesincronizat
    because we transfered Ownership
  • Nesincronizat
    it doesn't exist anymore
  • Nesincronizat
    the compiler knows this
  • Nesincronizat
    and can say
  • Nesincronizat
    "No, that is not safe what
    you're trying to do".
  • Nesincronizat
    and you'll note the link up here
  • Nesincronizat
    actually goes to a play ground
  • Nesincronizat
    and there's a sample for this code
  • Nesincronizat
    that you can experiment with there
  • Nesincronizat
    already prepared for you
  • Nesincronizat
    so
  • Nesincronizat
    we understand Borrowing
    a little bit better now
  • Nesincronizat
    especially with the analogy
  • Nesincronizat
    but how does this actually
  • Nesincronizat
    help in practice?
  • Nesincronizat
    how does this address the
    2 cases we talked about before?
  • Nesincronizat
    using values after they're no longer valid
  • Nesincronizat
    and writing code that works with
    multiple threads at the same time
  • Nesincronizat
    so first
  • Nesincronizat
    with Use-after-free problems
  • Nesincronizat
    those happen in C++
  • Nesincronizat
    because you'll have pointers in your code
  • Nesincronizat
    but the pointers are not associated
  • Nesincronizat
    with the values they're pointing to
  • Nesincronizat
    so you could delete
  • Nesincronizat
    the memory that they're pointing to
  • Nesincronizat
    but the pointers are still
    pointing at that memory
  • Nesincronizat
    and so if you aren't careful
  • Nesincronizat
    you can end up using pointers
  • Nesincronizat
    that are pointing to invalid memory
  • Nesincronizat
    so in Rust
  • Nesincronizat
    the compiler is actually tracking this
  • Nesincronizat
    so it doesn't let you write code
  • Nesincronizat
    that has the potential
  • Nesincronizat
    where pointers could point at memory
  • Nesincronizat
    that no longer is valid
  • Nesincronizat
    and that means that
  • Nesincronizat
    you're always taking
  • Nesincronizat
    borrowed pointers to values
  • Nesincronizat
    where the compiler says
  • Nesincronizat
    "OK. I know exactly how long
    this value is valid for because
  • Nesincronizat
    I know what the owner is
  • Nesincronizat
    and therefore any pointers to it
  • Nesincronizat
    must no longer exist
  • Nesincronizat
    before the owner no longer exists".
  • Nesincronizat
    and additionally
  • Nesincronizat
    you can also
  • Nesincronizat
    because the Rust compiler also tracks
  • Nesincronizat
    outstanding Borrows
  • Nesincronizat
    you can also say
  • Nesincronizat
    "OK we are guaranteed that there's no way
  • Nesincronizat
    to mutate this while anyone else is
    borrowing and trying to look at the value
  • Nesincronizat
    and this addresses another case
  • Nesincronizat
    where this can happen in C++
  • Nesincronizat
    where you end up changing the value
  • Nesincronizat
    like reallocating a pointer or something
  • Nesincronizat
    while other things are trying to point to
  • Nesincronizat
    memory that it was relying on
  • Nesincronizat
    and as for parallel code
  • Nesincronizat
    with multiple threads
  • Nesincronizat
    the issue arises with Data Races
  • Nesincronizat
    where you have multiple threads
  • Nesincronizat
    that are sharing values
  • Nesincronizat
    and they're also mutating them
  • Nesincronizat
    it's the same issue we
    just talked about basically
  • Nesincronizat
    where
  • Nesincronizat
    they're not coordinated
  • Nesincronizat
    in figuring out
  • Nesincronizat
    when it's safe to write to a value
  • Nesincronizat
    and when it's safe to read from a value
  • Nesincronizat
    because you might get intermediate values
  • Nesincronizat
    when the write hasn't completed yet
  • Nesincronizat
    so Rust addresses this by
  • Nesincronizat
    forcing you to either transfer
    ownership of values
  • Nesincronizat
    that another thread needs to use
  • Nesincronizat
    so that 2 threads cannot
    share the same value
  • Nesincronizat
    or
  • Nesincronizat
    ensuring that any values that you do
  • Nesincronizat
    share between the threads
  • Nesincronizat
    abide by certain restrictions
  • Nesincronizat
    that prevent invalid writes from happening
  • Nesincronizat
    and invalid reads from happening
  • Nesincronizat
    so that races can't happen
  • Nesincronizat
    and it's impossible to write code that
    is not actually thread safe
  • Nesincronizat
    so
  • Nesincronizat
    how this actually applies
  • Nesincronizat
    to what we're doing at Mozilla
  • Nesincronizat
    is that
  • Nesincronizat
    we've got this little organisation
  • Nesincronizat
    and so part of them are working on
  • Nesincronizat
    improving Rust the language
  • Nesincronizat
    however another part of them
  • Nesincronizat
    are working on building a
    brand new web browser
  • Nesincronizat
    because Firefox is not enough
  • Nesincronizat
    this one's called Servo
  • Nesincronizat
    and so we're building it
    from the ground up
  • Nesincronizat
    and the reason we're doing this
  • Nesincronizat
    is that
  • Nesincronizat
    there are decisions that were made
  • Nesincronizat
    design decisions for Firefox
  • Nesincronizat
    that were made a decade ago
  • Nesincronizat
    which are very difficult to
    change at this point in time
  • Nesincronizat
    it'd be a huge amount of work
  • Nesincronizat
    to redesign parts of Firefox
  • Nesincronizat
    we think could be better
  • Nesincronizat
    but it's also difficult
  • Nesincronizat
    to verify whether that would be
  • Nesincronizat
    a worthwhile use of time
  • Nesincronizat
    without actually doing it
  • Nesincronizat
    so we compromise by creating
    a brand new browser
  • Nesincronizat
    where we can actually try out new ideas
  • Nesincronizat
    and give ourselves a playground
    to experiment
  • Nesincronizat
    with things we could do better in Firefox
  • Nesincronizat
    and if they work out
  • Nesincronizat
    great
  • Nesincronizat
    we can then start transferring
    them back into Firefox
  • Nesincronizat
    and integrate them
  • Nesincronizat
    and additionally
  • Nesincronizat
    this means that we can experiment
  • Nesincronizat
    with letting out pages
  • Nesincronizat
    doing that in parallel
  • Nesincronizat
    rather than all other browsers today
  • Nesincronizat
    which do them sequentially
    and single-thread
  • Nesincronizat
    we can do things
  • Nesincronizat
    like experiment with the way
  • Nesincronizat
    that we
  • Nesincronizat
    draw things to the screen
  • Nesincronizat
    and do it more like a computer game
  • Nesincronizat
    we can do things like change the way
  • Nesincronizat
    that we're managing memory
  • Nesincronizat
    for the parts of the web page
  • Nesincronizat
    so that it's more integrated
    with the garbage collection
  • Nesincronizat
    that the Javascript code
  • Nesincronizat
    ends up ineracting with
  • Nesincronizat
    so all of this are things
    that we're doing in Servo
  • Nesincronizat
    and we're getting some great results
  • Nesincronizat
    and some of them are already
  • Nesincronizat
    being integrated back into Firefox
  • Nesincronizat
    so we're already shipping foot code today
  • Nesincronizat
    in Firefox that's written in Rust
  • Nesincronizat
    for example we have
    parts of our Media Stack
  • Nesincronizat
    we have code that's just
  • Nesincronizat
    responsible for reading parts
  • Nesincronizat
    of video files
  • Nesincronizat
    which has been a source
    of security vulnerabilities
  • Nesincronizat
    in the past
  • Nesincronizat
    and there's no reason that
  • Nesincronizat
    should be the case
Title:
Josh Matthews - An Introduction To Rust
Description:

more » « less
Video Language:
English
Team:
Mozilla
Proiect:
Rust
Duration:
28:19

English subtitles

Versiuni Compare revisions