From 295b343aecf330e830d79f06e0efc511e7d76da1 Mon Sep 17 00:00:00 2001 From: Jasper Ras Date: Tue, 5 Aug 2025 08:53:21 +0200 Subject: vault backup: 2025-08-05 08:53:21 --- Creating a reference to a variable is called borrowing.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Creating a reference to a variable is called borrowing.md (limited to 'Creating a reference to a variable is called borrowing.md') diff --git a/Creating a reference to a variable is called borrowing.md b/Creating a reference to a variable is called borrowing.md new file mode 100644 index 0000000..245d060 --- /dev/null +++ b/Creating a reference to a variable is called borrowing.md @@ -0,0 +1,13 @@ +[[Rust]] +[[References are also immutable by default]] +[[Mutable references are safe because they prevent aliasing]] +[[Ownership]] +[[Dereferencing is to access the data behind a pointer]] + +--- + +Creating a reference is done with an ampersand. It creates a pointer to another variable in the stack, which could be a pointer to something on the heap. + +This is called borrowing. + +A reference is a non-owning pointer; so if we create a reference to a Box we can safely have it discarded and continue using the box because the heap data is not deallocated. \ No newline at end of file -- cgit v1.2.3