Superteam Academy
|bySuperteam BrasilSuperteam Brasil
Superteam Academy

The decentralized learning platform for Solana developers.

Resources

  • Courses
  • Leaderboard
  • Community

Support

  • Documentation
  • API Reference
  • Community

Legal

  • Terms of Service
  • Privacy Policy
  • Cookie Policy

Follow Us

  • Twitter / X
  • Discord
  • GitHub

Stay in the Loop

Get weekly updates on new courses, Solana ecosystem news, and learning tips.

Powered bySuperteam BrasilSuperteam Brasil

Β© 2026 Superteam Academy. All rights reserved.

Rust Ownership & Memory

References and Borrowing
Ownership Challenge
Borrowing & Lifetimes / References and Borrowing
30 XP

References and Borrowing

Borrowing

Instead of transferring ownership, you can borrow a value using references.

Immutable References (&T)

Rust
fn calculate_length(s: &String) -> usize {
    s.len()
} // s goes out of scope, but since it doesn't own the String, nothing is dropped

let s1 = String::from("hello");
let len = calculate_length(&s1);
println!("{s1} has length {len}"); // s1 is still valid

You can have multiple immutable references at the same time.

Mutable References (&mut T)

Rust
fn push_world(s: &mut String) {
    s.push_str(" world");
}

let mut s = String::from("hello");
push_world(&mut s);
println!("{s}"); // "hello world"

The Borrowing Rules

  1. You can have either many &T or one &mut T β€” never both at the same time
  2. References must always be valid (no dangling pointers)

This is how Rust prevents data races at compile time β€” no runtime locks needed.

Slices: Borrowed Views

Rust
let s = String::from("hello world");
let hello: &str = &s[0..5];
let world: &str = &s[6..11];
println!("{hello} {world}");

Slices borrow a portion of data without copying.

You need to enroll in this course before you can mark lessons as complete.
2 / 3