Rust has a lot of different pointer types, reminding me of the C++ references, raw and smart pointers like std::unique_ptr, std::shared_ptr. For a nice comparison see here.

References

One of the first pointer type you will meet in Rust is the reference type.

fn main () { let mut x = 1 ; println ! ( "x = {}" , x ); add_one ( & mut x ); println ! ( "x = {}" , x ); } fn add_one ( num : & mut i32 ) { * num += 1 ; }

In C++ this would look something like this:

#include <cstdio> void add_one ( int & num ) { num += 1 ; } int main () { int x = 1 ; printf ( "x = %i

" , x ); add_one ( x ); printf ( "x = %i

" , x ); }

What is think is nice in Rust is:

It's explicit that x will be modified ( &mut x )

) It's guaranteed that x is neither dangling nor 0

In C++ references have value syntax. To avoid that I prefer the style of using pointers for mutable and only const reference for immutable data.

#include <cstdio> void add_one ( int * num ) { * num += 1 ; } int main () { int x = 1 ; printf ( "x = %i

" , x ); add_one ( & x ); printf ( "x = %i

" , x ); }

This way the syntax looks much more like the Rust example, but you can pass 0:

add_one ( 0 ); -> Segmentation fault ( core dumped )

Trying the same with C++ references yields a compile error, because a non const reference can't bind to a temporary (rvalue)

reference.cpp: In function 'int main()': reference.cpp:12:11: error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int' add_one(0); ^ reference.cpp:3:6: note: in passing argument 1 of 'void add_one(int&)' void add_one(int& num) { ^

Surprising to me, doing the same within Rust works

fn main () { add_one ( & mut 0 ); } fn add_one ( num : & mut i32 ) { print ! ( "adding one to {}" , num ); * num += 1 ; println ! ( " -> {}" , num ); }