From d157b54fd4e9062705a2f40049d1dffa02aa13df Mon Sep 17 00:00:00 2001 From: Fabian Bornhofen Date: Tue, 10 Jan 2023 15:04:17 +0100 Subject: [PATCH] Add speaker notes about RVO in borrowing.md --- src/ownership/borrowing.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/ownership/borrowing.md b/src/ownership/borrowing.md index 36cf27c0..cb104645 100644 --- a/src/ownership/borrowing.md +++ b/src/ownership/borrowing.md @@ -21,3 +21,31 @@ fn main() { * The `add` function _borrows_ two points and returns a new point. * The caller retains ownership of the inputs. + +
+ +Notes on stack returns: +* Demonstrate that the return from `add` is cheap because the compiler can eliminate the copy operation. Change the above code to print stack addresses and run it on the [Playground](https://play.rust-lang.org/). In the "DEBUG" optimization level, the addresses should change, while the stay the same when changning to the "RELEASE" setting: + + ```rust,editable + #[derive(Debug)] + struct Point(i32, i32); + + fn add(p1: &Point, p2: &Point) -> Point { + let p = Point(p1.0 + p2.0, p1.1 + p2.1); + println!("&p.0: {:p}", &p.0); + p + } + + fn main() { + let p1 = Point(3, 4); + let p2 = Point(10, 20); + let p3 = add(&p1, &p2); + println!("&p3.0: {:p}", &p3.0); + println!("{p1:?} + {p2:?} = {p3:?}"); + } + ``` +* The Rust compiler can do return value optimization (RVO). +* In C++, copy elision has to be defined in the language specification because constructors can have side effects. In Rust, this is not an issue at all. + +