From 480132b3ad8a69fb9f9261ddf35072aa63e3782e Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 16 May 2019 01:33:13 +0100 Subject: [PATCH] wip --- Makefile | 2 +- dangling-err.txt | 9 +++++++++ dangling.fig | 7 ++++++- dangling.txt | 11 ----------- talk.txt | 8 ++++---- 5 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 dangling-err.txt diff --git a/Makefile b/Makefile index eeb8936..f9a9c2e 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ mm0.ps: mm.fig bck-lifetimes.ps: bck-err.txt.eps macros.ps: serde-example.txt.eps macro-rules-example.txt.eps borrow-mut.ps: borrow-mut-example.txt.eps -dangling.ps: dangling.txt.eps +dangling.ps: dangling.txt.eps dangling-err.txt.eps %.eps: %.fig iconv <$< >$@.1 -f UTF-8 -t ISO-8859-1 diff --git a/dangling-err.txt b/dangling-err.txt new file mode 100644 index 0000000..f2142d2 --- /dev/null +++ b/dangling-err.txt @@ -0,0 +1,9 @@ +error[E0106]: missing lifetime specifier + --> main.rs:5:16 + | +5 | fn dangle() -> &String { + | ^ expected lifetime parameter + | + = help: this function's return type contains a borrowed + value, but there is no value for it to be borrowed from + = help: consider giving it a 'static lifetime diff --git a/dangling.fig b/dangling.fig index 30b4920..cf30050 100644 --- a/dangling.fig +++ b/dangling.fig @@ -9,4 +9,9 @@ Single 1200 2 2 5 0 1 0 -1 60 -1 -1 0.000 0 0 -1 0 0 5 0 dangling.txt.eps - -1170 -2250 10593 -2250 10593 3993 -1170 3993 -1170 -2250 + -1170 -2250 10593 -2250 10593 1625 -1170 1625 -1170 -2250 +2 5 0 1 0 -1 60 -1 -1 0.000 0 0 -1 0 0 5 + 0 dangling-err.txt.eps + -1170 2155 10593 2155 10593 5080 -1170 5080 -1170 2155 +2 1 0 2 0 7 50 -1 -1 0.000 0 0 7 0 0 2 + -1260 1890 10800 1890 diff --git a/dangling.txt b/dangling.txt index e4b884c..b27b01a 100644 --- a/dangling.txt +++ b/dangling.txt @@ -6,14 +6,3 @@ fn dangle() -> &String { let s = String::from("hello"); &s } - - -error[E0106]: missing lifetime specifier - --> main.rs:5:16 - | -5 | fn dangle() -> &String { - | ^ expected lifetime parameter - | - = help: this function's return type contains a borrowed - value, but there is no value for it to be borrowed from - = help: consider giving it a 'static lifetime diff --git a/talk.txt b/talk.txt index 2e5772d..3e7f9bb 100644 --- a/talk.txt +++ b/talk.txt @@ -55,6 +55,8 @@ many ways this resembles informal notions of object ownership which programmers have typically used in C and C++ to make their code comprehensible. +[ Borrowing example slide ] https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references + But in Rust the ownership is known to the language, and the rules surrounding ownership are enforced by the compiler. When an object ceases to have an owner, it is freed. @@ -62,21 +64,19 @@ ceases to have an owner, it is freed. This ownership system is used not only for dynamically allocated objects, but for local variables, and statically allocated objects. -[ Borrowing example slide ] https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references +[ dangling reference error example https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#dangling-references ] To allow an object to be accessed other than through owner, Rust formalises the notion of borrowing. An object can be borrowed by writing the ampersand, giving a reference. The compiler checks that the object will outlive the reference. -[ dangling reference error example https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#dangling-references ] +[ Borrowing example slide with mut missing ] ABOVE https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references Borrows (that is, references) come in two kinds: mutable and immutable. You can have many immutable references at once. A mutable reference permits modification, and provides exclusive access. -[ Borrowing example slide with mut missing ] ABOVE https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references - That's what the `mut' is doing in that example. If you leave it out the compiler complains. This is the Rust `borrow checker' that you have may have heard about: the part of the compiler which enforces the -- 2.30.2