chiark / gitweb /
wip
[talk-2019-ghm-rust.git] / talk.txt
1
2 In many respects Rust is a competent but not particularly novel
3 language.
4
5 Powerful.
6
7 Reference to Blub languages.
8
9 B======================================================================B
10
11 [ MM survey slide ]
12
13 Firstly, I'm going to go into some detail about Rust's one very novel
14 property: it's approach to memory management.  Let us consider,
15 particularly, dynamically allocated memory.
16
17 Until now there have been basically two approaches:
18
19 In lower-level languages like C, C++ and assembler, you as the
20 programmer must know whenever memory allocation is occurring, and
21 control when that memory is freed, usually with explicit code.
22 Mistakes lead to bugs - often, bugs which are very hard to track down.
23 There are some static checkers, and coding conventions, which help to
24 reduce the number of these bugs but they are still a big problem even
25 with the most careful software development practices.
26
27 In higher-level languages, memory allocation is often implicit and
28 deallocation is handled automatically by a garbage collector.  This is
29 convenient for the programmer.  But it requires support from a
30 language runtime, which is awkward in some environments and makes it
31 hard to interface to other languages.  And it costs performance -
32 sometimes, a lot of performance.  Traditionally, garbage-collected
33 languages have focused on programmer convenience, or programming
34 language power, rather than performance.
35
36 [ MM survey slide + Rust ]
37
38 Rust has a different approach.  Each object in a Rust program has
39 exactly one owner; for a local variable that owner is the variable.
40 For an object in a complex data structure, the data structure is the
41 owner (and of course that data structure has, in turn, an owner).  In
42 many ways this resembles informal notions of object ownership which
43 programmers have typically used in C and C++ to make their code
44 comprehensible.
45
46 But in Rust the ownership is known to the language, and the rules
47 surrounding ownership are enforced by the compiler.  When an object
48 ceases to have an owner, it is freed.
49
50 This ownership system is used not only for dynamically allocated
51 objects, but for local variables, and statically allocated objects.
52
53 [ Borrowing example slide ]  https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references
54
55 To allow an object to be accessed other than through owner, Rust
56 formalises the notion of borrowing.  An object can be borrowed by
57 writing the ampersand, giving a reference.  The compiler checks that
58 the object will outlive the reference.
59
60 [ dangling reference error example  https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#dangling-references ]
61
62 Borrows (that is, references) come in two kinds: mutable and
63 immutable.  You can have many immutable references at once.  A mutable
64 reference permits modification, and provides exclusive access.
65
66 [ Borrowing example slide ]  https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references
67
68 That's what the `mut' is doing in that example.
69
70
71
72
73 You can lend Objects which 
74
75
76 This ownership is
77 known to the 
78
79
80 tends to lead to 
81
82
83
84 write explicit code allocate memory, and and free allre