From 6bda89f34e6cdc35c26066c67265e62f20e89f01 Mon Sep 17 00:00:00 2001 From: Marko Zagar Date: Thu, 12 Jan 2023 18:57:33 +0100 Subject: [PATCH] Clarification on references --- src/basic-syntax/references.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/basic-syntax/references.md b/src/basic-syntax/references.md index 02ebc553..0bb64fdf 100644 --- a/src/basic-syntax/references.md +++ b/src/basic-syntax/references.md @@ -11,9 +11,18 @@ fn main() { } ``` -Some differences from C++: +Some notes: -* We must dereference `ref_x` when assigning to it, similar to C pointers, +* We must dereference `ref_x` when assigning to it, similar to C and C++ pointers. * Rust will auto-dereference in some cases, in particular when invoking - methods (try `count_ones`). + methods (try `ref_x.count_ones()`). * References that are declared as `mut` can be bound to different values over their lifetime. + +
+Key points: + +* Be sure to note the difference between `let mut ref_x: &i32` and `let ref_x: + &mut i32`. The first one represents a mutable reference which can be bound to + different values, while the second represents a reference to a mutable value. + +