0

Rust – Mutable Borrowing vs Shared Borrowing

There are two types of borrowing in Rust – mutable and shared borrowing.

Mutable Borrowing

This means Rust shares the ownership of variables and the values are mutable. Later, Rust returns the ownership returns back to the owner possibly with modified value.

Example 1 – Modify Integer Value

This outputs

Example 2 – Modify Vect Contents

This outputs

Shared Borrowing

This means Rust shares the ownership of variables and the values are immutable. Later, Rust returns the ownership returns back to the owner with the same value.

Example 3 – Share Integer Value

This outputs

For more information, please see https://doc.rust-lang.org/1.30.0/book/2018-edition/ch04-00-understanding-ownership.html.

Karl San Gabriel

Karl San Gabriel

Professional Software Developer