Borrowing and references in Rust

Learn Rust · Beginner · 10 min · After Ownership and moves in Rust

How do you use a value without taking ownership of it?

  1. 1Guess

    fn len(s: &String) -> usize {
        s.len()
    }
    
    fn main() {
        let s = String::from("hello");
        let n = len(&s);
        println!("{s} has {n} bytes");
    }
    
    What does this program print?
  2. 2Read

    References borrow

    A reference lets you use a value without owning it. &s creates one, and the type &String or &str accepts it. The owner keeps the value, and nothing is dropped when the reference goes out of scope. The Book calls creating a reference borrowing. To change what you borrowed, you need a mutable reference, &mut s, and the variable must be declared mut.

  3. 3Code

    A shared and a mutable borrow

    1 / 2
    fn main() {
        let mut name = String::from("Ferris");
        greet(&name);
        add_title(&mut name);
        greet(&name);
    }
    
    fn greet(who: &str) {
        println!("Hello, {who}!");
    }
    
    fn add_title(who: &mut String) {
        who.push_str(" the Crab");
    }
    

    Read the program. What will it print?

    Run it in the playground

  4. 4Read

    The borrowing rule

    At any given time you can have either one mutable reference or any number of immutable references to a value. References must also always be valid, so one cannot outlive the value it points to. A reference lasts from where it is created to where it is last used, not to the end of the block.

  5. 5Code

    A mutable borrow after the shared ones end

    1 / 2
    fn main() {
        let mut s = String::from("hello");
        let r1 = &s;
        let r2 = &s;
        println!("{r1} and {r2}");
        let r3 = &mut s;
        r3.push_str(" world");
        println!("{r3}");
    }
    

    Read the program. What will it print?

    Run it in the playground

  6. 6Fix

    fn main() {
        let mut s = String::from("hello");
        let r1 = &s;
        let r2 = &mut s;
        println!("{r1} {r2}");
    }
    
    This program does not compile. What is the bug?
  7. 7Say it back

  8. 8Mission · 8 min

    Write a function that takes &mut String and appends text to it, and another that takes &str and prints it. Call both from main, then try holding a shared reference across the mutable call and read the error.

    cargo new borrow
    cd borrow
    cargo run

Common questions

What is borrowing in Rust?
Borrowing means using a value through a reference instead of taking ownership of it. You write &value to lend it for reading, or &mut value to lend it for changing. The owner keeps the value, and it is not dropped when the reference ends, so you can keep using the original afterwards.
Why can't I have two mutable references in Rust?
The borrowing rule allows either one mutable reference or any number of immutable ones at a time. Two mutable references to the same value could change it at once, so the compiler refuses them. A reference lasts until its last use, so you can take a new one after the first is no longer used.
What is the difference between &str and &String?
Both are references to text. A &String borrows a whole String, while &str is a string slice that borrows a part or all of some text. A function that takes &str is more flexible, because you can pass it a reference to a String as well as a string literal.

Sources

Measured with rustc 1.100.1 (play.rust-lang.org, stable, edition 2024) on 2026-12-03

Published Dec 3, 2026 · Updated Dec 3, 2026

Never miss new offers and news

New tools, credits, and AI news are added regularly. Pick how you want to hear about them.

Telegram channel

Join @aifree_dev and get new free AI offers and news right in your Telegram feed.

Browser notifications

Turn on push notifications and get an alert on this device whenever something new is posted. No account needed.

Email newsletter

A weekly digest of new, changed and expiring free AI offers. Double opt-in, one-click unsubscribe. See the latest issue.

Unsubscribe anytime. Privacy

Frequently asked questions

What is aifree.dev?

aifree.dev helps everyone access AI through a curated directory of free AI tools, API credits, free tiers, and startup programs for coding, image, voice, video, and students - verified regularly.

How are offers presented?

Each offer includes its access details, free models when available, and expiration information when provided. Limited-time deals stay visible so they are easy to compare.

Do education and startup programs need verification?

Yes. Providers usually ask students to verify their status with a school email, student ID, or a program like the GitHub Student Pack, and ask startups to verify company details such as age, funding, or website. Check each offer’s claim page for the exact requirements.

What kinds of free AI offers are listed?

Free tiers and free trials from AI providers, free API credits, startup and student programs, and free tools for coding, image generation, voice, and video.

How can I suggest a tool or report a broken link?

Use the submit form: paste the offer page URL and a short note. A person checks every submission before anything is listed. Broken links and changed terms go through the same form.

Know a good one?

Tell us about an AI tool worth knowing. Found a broken link? We want to hear that too.

Submit an offer