Ownership and moves in Rust

Learn Rust · Beginner · 10 min · After Variables, mut and shadowing in Rust

Who owns a value in Rust, and when is it freed?

  1. 1Guess

    fn main() {
        let a = 5;
        let b = a;
        println!("{a} {b}");
    }
    
    What does this program print?
  2. 2Read

    The ownership rules

    Rust manages memory with three rules the compiler checks. Each value has an owner. There can only be one owner at a time. When the owner goes out of scope, the value is dropped. Dropped means Rust frees the memory for you, at the closing brace of the scope. You write no free call and there is no garbage collector.

  3. 3Code

    A String and a function that returns it

    1 / 2
    fn main() {
        let mut s = String::from("hello");
        s.push_str(", world");
        println!("{s}");
        let t = take_and_give_back(s);
        println!("{t}");
    }
    
    fn take_and_give_back(text: String) -> String {
        println!("got {text}");
        text
    }
    

    Read the program. What will it print?

    Run it in the playground

  4. 4Read

    Move, Copy and clone

    Assigning a String to another variable moves it: the new variable owns the data and the old one can no longer be used. Types with a fixed size, such as integers, implement Copy and are copied instead. If you want a second, independent String, call .clone(). It copies the data, so use it when you need both.

  5. 5Code

    clone, then move into a function

    1 / 2
    fn main() {
        let s1 = String::from("hello");
        let s2 = s1.clone();
        println!("{s1} and {s2}");
        let len = calculate_length(s2);
        println!("length {len}");
    }
    
    fn calculate_length(s: String) -> usize {
        s.len()
    }
    

    Read the program. What will it print?

    Run it in the playground

  6. 6Fix

    fn main() {
        let s1 = String::from("hello");
        let s2 = s1;
        println!("{s1}, world!");
        println!("{s2}");
    }
    
    This program does not compile. What is the bug?
  7. 7Say it back

  8. 8Mission · 8 min

    Write a program that makes a String, moves it into a function that prints it and returns its length, and prints the length. Then try using the String after the call and read the error.

    cargo new owners
    cd owners
    cargo run

Common questions

What is ownership in Rust?
Ownership is the set of rules Rust uses to manage memory without a garbage collector. Each value has one owner, there can be only one owner at a time, and when the owner goes out of scope the value is dropped. The compiler checks these rules, so a program that breaks them does not compile.
What does move mean in Rust?
A move transfers ownership of a value. When you write let s2 = s1 with a String, s2 becomes the owner and s1 can no longer be used; the compiler reports an error if you try. Passing a String to a function moves it too. Simple types such as integers are copied instead of moved.
When should I use clone in Rust?
Call clone when you need a second, independent copy of a value such as a String, and both copies must stay usable. It copies the data, so it costs time and memory for large values. If you only need to read a value in a function, borrow it with a reference instead of cloning.

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