Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

  • Reference
  • 30 errors

Rust compiler errors, explained

Each entry is the smallest program that triggers one code, the real output, and the fix that compiles. Re-checked on every build.

30 of 30 errors

Names, calls and privacy 5

The compiler could not find what you named, or you called it the wrong way.

E0425This name does not exist here

Rust cannot find anything with that name at this spot in your program. A name is the label you give a value with let, like bot_name. Usually it is a typo, or a name made inside curly braces { } that have already closed: a name only lives until the closing brace of the block it was made in.

fn main() {
let bot_name = "Chatterbox";
println!("{}", bot_nmae);
}
error[E0425]: cannot find value `bot_nmae` in this scope
 --> src/main.rs:3:20
  |
3 |     println!("{}", bot_nmae);
  |                    ^^^^^^^^
  |
help: a local variable with a similar name exists
  |
3 -     println!("{}", bot_nmae);
3 +     println!("{}", bot_name);
  |

The usual fix. Compare the spelling letter by letter with the let line; the compiler often suggests the name you meant. If the name was made inside a block, make it before the block instead.

fn main() {
let bot_name = "Chatterbox";
println!("{}", bot_name);
}

Taught in: 1.3 Printing and comments

Link to E0425

E0433The compiler does not know where this comes from

Some ready-made tools, like HashMap (a lookup table that finds a value by its key), live in Rust's standard library but are not switched on automatically. You have to say where to find them with a use line at the top of the file. Until you do, Rust does not recognise the name.

fn main() {
let counts = HashMap::<String, u32>::new();
println!("{}", counts.len());
}
error[E0433]: cannot find type `HashMap` in this scope
 --> src/main.rs:2:18
  |
2 |     let counts = HashMap::<String, u32>::new();
  |                  ^^^^^^^ use of undeclared type `HashMap`
  |
help: consider importing this struct
  |
1 + use std::collections::HashMap;
  |

The usual fix. Add use std::collections::HashMap; at the top of the file. The help line under the error usually prints the exact use line to copy.

use std::collections::HashMap;
 
fn main() {
let counts = HashMap::<String, u32>::new();
println!("{}", counts.len());
}

Link to E0433

E0061Wrong number of inputs to a function

A function lists the inputs it needs, called arguments, in the brackets after its name. Every call must give exactly that many, in the same order. Here reply asks for two, the message and the bot's name, but the call gives only one.

fn reply(msg: &str, bot_name: &str) -> String {
format!("{bot_name}: you said {msg}")
}
 
fn main() {
let answer = reply("hi");
println!("{answer}");
}
error[E0061]: this function takes 2 arguments but 1 argument was supplied
 --> src/main.rs:6:18
  |
6 |     let answer = reply("hi");
  |                  ^^^^^------ argument #2 of type `&str` is missing
  |
note: function defined here
 --> src/main.rs:1:4
  |
1 | fn reply(msg: &str, bot_name: &str) -> String {
  |    ^^^^^            --------------
help: provide the argument
  |
6 |     let answer = reply("hi", /* &str */);
  |                            ++++++++++++

The usual fix. Put the call next to the fn line and count. Add the missing argument, reply("hi", "Chatterbox"), or remove the extra one. The compiler points at the argument you left out.

fn reply(msg: &str, bot_name: &str) -> String {
format!("{bot_name}: you said {msg}")
}
 
fn main() {
let answer = reply("hi", "Chatterbox");
println!("{answer}");
}

Taught in: 1.6 Functions

Link to E0061

E0616This field is private

A module is a named section of your code, often its own file, like message.rs. Inside a module everything is private by default: code outside it cannot touch a field unless the field is marked pub (short for public). Here main sits outside the message module and reaches for text, which the module kept to itself.

mod message {
pub struct Message {
text: String,
}
 
impl Message {
pub fn new(text: &str) -> Message {
Message { text: text.to_string() }
}
}
}
 
fn main() {
let msg = message::Message::new("hello");
println!("{}", msg.text);
}
error[E0616]: field `text` of struct `Message` is private
  --> src/main.rs:15:24
   |
15 |     println!("{}", msg.text);
   |                        ^^^^ private field

The usual fix. Either mark the field pub, if outside code may read and change it freely, or (usually better) add a pub fn method that hands it out, like msg.text(), so the module stays in charge of its own data.

mod message {
pub struct Message {
text: String,
}
 
impl Message {
pub fn new(text: &str) -> Message {
Message { text: text.to_string() }
}
 
pub fn text(&self) -> &str {
&self.text
}
}
}
 
fn main() {
let msg = message::Message::new("hello");
println!("{}", msg.text());
}

Taught in: 8.1 Splitting code into files

Link to E0616

E0599This type has no method with that name

A method is a function that belongs to a type and is called with a dot, like history.push(msg). The type you called it on has no method with that name, usually because the name is a near-miss guess. A Vec (Rust's list that can grow) adds an item with push, not add.

fn main() {
let mut history: Vec<String> = Vec::new();
history.add(String::from("hi"));
}
error[E0599]: no method named `add` found for struct `Vec<String>` in the current scope
 --> src/main.rs:3:13
  |
3 |     history.add(String::from("hi"));
  |             ^^^ method not found in `Vec<String>`

The usual fix. Read the help line: the compiler often names the method you meant. In your editor, type the dot and wait for the list of real methods. If the value is an Option or Result (a wrapper that may or may not hold a value), take the value out first.

fn main() {
let mut history: Vec<String> = Vec::new();
history.push(String::from("hi"));
println!("{}", history.len());
}

Link to E0599

Types and choices 6

Every value has a type, and every option of a choice needs handling.

E0308The value is the wrong type

Every value in Rust has a type: a whole number, a piece of text, true or false, and so on. When a name is meant to hold one type and you give it another, Rust stops; it never quietly turns one into the other. Here "3" in quotes is text, not the number 3. The message lists what it expected and what it found.

fn main() {
let count: u32 = "3";
println!("{count}");
}
error[E0308]: mismatched types
 --> src/main.rs:2:22
  |
2 |     let count: u32 = "3";
  |                ---   ^^^ expected `u32`, found `&str`
  |                |
  |                expected due to this

The usual fix. Read the expected / found pair. Give a value of the right type (3 without quotes), change the type you wrote, or convert on purpose: "3".parse().unwrap() turns text into a number.

fn main() {
let count: u32 = 3;
println!("{count}");
}

Taught in: 1.5 Reading what the compiler says

Link to E0308

E0282The compiler cannot tell what type this is

Rust usually works out a value's type by itself from how you use it. Here history is an empty list and nothing is ever put in it, so Rust cannot tell whether it is a list of numbers, of text, or of something else. It needs one hint.

fn main() {
let history = Vec::new();
println!("{}", history.len());
}
error[E0282]: type annotations needed for `Vec<_>`
 --> src/main.rs:2:9
  |
2 |     let history = Vec::new();
  |         ^^^^^^^   ---------- type must be known at this point
  |
help: consider giving `history` an explicit type, where the type for type parameter `T` is specified
  |
2 |     let history: Vec<T> = Vec::new();
  |                ++++++++

The usual fix. Say the type once: let history: Vec<String> = Vec::new();. Putting an item in later, like history.push(msg), also gives Rust the hint.

fn main() {
let history: Vec<String> = Vec::new();
println!("{}", history.len());
}

Link to E0282

E0063A struct is missing a field

A struct bundles several named values, called fields, into one, like a Message with an author and a text. When you build one you must fill in every field; there is no leaving one blank. If a field may really have nothing in it, its type is Option (either Some(value) or None) and you write None.

struct Message {
author: String,
text: String,
}
 
fn main() {
let msg = Message { author: String::from("ana") };
println!("{}", msg.author);
}
error[E0063]: missing field `text` in initializer of `Message`
 --> src/main.rs:7:15
  |
7 |     let msg = Message { author: String::from("ana") };
  |               ^^^^^^^ missing `text`

The usual fix. Add the field the error names: text: String::from("hello").

struct Message {
author: String,
text: String,
}
 
fn main() {
let msg = Message {
author: String::from("ana"),
text: String::from("hello"),
};
println!("{}: {}", msg.author, msg.text);
}

Link to E0063

E0004A match does not handle every case

An enum is a type that is exactly one of a fixed list of options, like a Command that is Help, Quit or History. A match must say what to do for every option, and this one forgot History. That check is what makes adding a new command safe: Rust lists every place that still needs updating.

enum Command {
Help,
Quit,
History,
}
 
fn run(cmd: Command) {
match cmd {
Command::Help => println!("Type a message, or /quit."),
Command::Quit => println!("Bye!"),
}
}
 
fn main() {
run(Command::History);
}
error[E0004]: non-exhaustive patterns: `Command::History` not covered
  --> src/main.rs:8:11
   |
 8 |     match cmd {
   |           ^^^ pattern `Command::History` not covered
   |
note: `Command` defined here
  --> src/main.rs:1:6
   |
 1 | enum Command {
   |      ^^^^^^^
...
 4 |     History,
   |     ------- not covered
   = note: the matched value is of type `Command`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
   |
10 ~         Command::Quit => println!("Bye!"),
11 ~         Command::History => todo!(),
   |

The usual fix. Add an arm for the missing option: Command::History => …. If you really mean "everything else", end with a _ => … arm.

enum Command {
Help,
Quit,
History,
}
 
fn run(cmd: Command) {
match cmd {
Command::Help => println!("Type a message, or /quit."),
Command::Quit => println!("Bye!"),
Command::History => println!("(no messages yet)"),
}
}
 
fn main() {
run(Command::History);
}

Taught in: 5.5 Patterns

Link to E0004

E0369Two pieces of quoted text cannot be joined with +

Text in quotes, like "Welcome", is a fixed piece of text (its type is &str) that cannot grow. + only works when the left side is a String, Rust's growable text, because the right side is added onto its end. Two fixed pieces have nothing to grow, so + is not allowed between them.

fn main() {
let banner = "Welcome to " + "Chatterbox";
println!("{banner}");
}
error[E0369]: cannot add `&str` to `&str`
 --> src/main.rs:2:32
  |
2 |     let banner = "Welcome to " + "Chatterbox";
  |                  ------------- ^ ------------ &str
  |                  |             |
  |                  |             `+` cannot be used to concatenate two `&str` strings
  |                  &str
  |
  = note: string concatenation requires an owned `String` on the left
help: create an owned `String` from a string reference
  |
2 |     let banner = "Welcome to ".to_owned() + "Chatterbox";
  |                               +++++++++++

The usual fix. Use format!("{}{}", a, b), which builds a new String, or make the left side a String first: String::from("Welcome to ") + "Chatterbox".

fn main() {
let banner = format!("{}{}", "Welcome to ", "Chatterbox");
println!("{banner}");
}

Taught in: 8.3 Code for any type: generics

Link to E0369

E0072A type that contains itself is endlessly big

Rust needs to know how much memory each type takes. A Message that directly holds another Message, which holds another, and so on, would be endlessly big. The course does not cover types that contain themselves; the idea is to keep the inner one in a Box (a small, fixed-size pointer to a value stored elsewhere), which the traits lesson uses.

struct Message {
text: String,
reply_to: Option<Message>,
}
 
fn main() {}
error[E0072]: recursive type `Message` has infinite size
 --> src/main.rs:1:1
  |
1 | struct Message {
  | ^^^^^^^^^^^^^^
2 |     text: String,
3 |     reply_to: Option<Message>,
  |                      ------- recursive without indirection
  |
help: insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle
  |
3 |     reply_to: Option<Box<Message>>,
  |                      ++++       +

The usual fix. Wrap the field in a box: reply_to: Option<Box<Message>>.

struct Message {
text: String,
reply_to: Option<Box<Message>>,
}
 
fn main() {
let msg = Message { text: String::from("hi"), reply_to: None };
println!("{} {}", msg.text, msg.reply_to.is_none());
}

Link to E0072

Values that change 3

A value stays the same unless its name was made with mut.

E0384Changing a value you did not mark as changeable

A name made with let keeps the same value forever, unless you say up front that it may change. That makes it easy to see which values move around in a program. Here count was made without that promise and then given a new value.

fn main() {
let count = 0;
count = count + 1;
println!("{count}");
}
error[E0384]: cannot assign twice to immutable variable `count`
 --> src/main.rs:3:5
  |
2 |     let count = 0;
  |         ----- first assignment to `count`
3 |     count = count + 1;
  |     ^^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
  |
help: consider making this binding mutable
  |
2 |     let mut count = 0;
  |         +++

The usual fix. Add mut (short for mutable, meaning changeable) where the name is made: let mut count = 0;.

fn main() {
let mut count = 0;
count = count + 1;
println!("{count}");
}

Taught in: 2.2 Values that change

Link to E0384

E0596Calling a changing method on a value that is not mut

Some methods change the value they are called on: push adds an item, so the list changes. You may only call those on a value whose name was made with let mut. The message says "borrow as mutable": to borrow is to use a value without taking it over, and mutable means with permission to change it.

fn main() {
let history: Vec<String> = Vec::new();
history.push(String::from("hi"));
}
error[E0596]: cannot borrow `history` as mutable, as it is not declared as mutable
 --> src/main.rs:3:5
  |
3 |     history.push(String::from("hi"));
  |     ^^^^^^^ cannot borrow as mutable
  |
help: consider changing this to be mutable
  |
2 |     let mut history: Vec<String> = Vec::new();
  |         +++

The usual fix. Make the name changeable: let mut history = ….

fn main() {
let mut history: Vec<String> = Vec::new();
history.push(String::from("hi"));
}

Link to E0596

E0594Changing a field of a value that is not mut

A struct's fields are the named values inside it, like the text of a Message. If the name holding the struct was made without mut, none of its fields can change either. It is the same rule as changing a plain value, one level down.

struct Message {
text: String,
}
 
fn main() {
let msg = Message { text: String::from("helo") };
msg.text = String::from("hello");
println!("{}", msg.text);
}
error[E0594]: cannot assign to `msg.text`, as `msg` is not declared as mutable
 --> src/main.rs:7:5
  |
7 |     msg.text = String::from("hello");
  |     ^^^^^^^^ cannot assign
  |
help: consider changing this to be mutable
  |
6 |     let mut msg = Message { text: String::from("helo") };
  |         +++

The usual fix. Make the whole value changeable: let mut msg = Message { … };.

struct Message {
text: String,
}
 
fn main() {
let mut msg = Message { text: String::from("helo") };
msg.text = String::from("hello");
println!("{}", msg.text);
}

Link to E0594

Ownership and moves 4

A value has one owner. These fire when you gave it away, or never had it.

E0382Using a value after you handed it away

Every value in Rust has exactly one owner; think of the owner as the one holder of its key. When you pass msg into a function, or assign it to another name, the key goes with it. That is called a move, and after it the old name holds nothing, so using it again is an error.

fn send(text: String) {
println!("sent: {text}");
}
 
fn main() {
let msg = String::from("hello");
send(msg);
println!("{msg}");
}
error[E0382]: borrow of moved value: `msg`
 --> src/main.rs:8:16
  |
6 |     let msg = String::from("hello");
  |         --- move occurs because `msg` has type `String`, which does not implement the `Copy` trait
7 |     send(msg);
  |          --- value moved here
8 |     println!("{msg}");
  |                ^^^ value borrowed here after move
  |
note: consider changing this parameter type in function `send` to borrow instead if owning the value isn't necessary
 --> src/main.rs:1:15
  |
1 | fn send(text: String) {
  |    ----       ^^^^^^ this parameter takes ownership of the value
  |    |
  |    in this function
help: consider cloning the value if the performance cost is acceptable
  |
7 |     send(msg.clone());
  |             ++++++++

The usual fix. Lend the value instead of handing it over: make the function take &str and call send(&msg). The & means "borrow it and give it back". If you really need two separate copies, use msg.clone().

fn send(text: &str) {
println!("sent: {text}");
}
 
fn main() {
let msg = String::from("hello");
send(&msg);
println!("{msg}");
}

Taught in: 4.3 Handing over the key

Link to E0382

E0505Handing a value away while it is lent out

To borrow a value is to use it through a reference, written with &, without owning it. While a borrow will still be used, the owner cannot give the value away, or the borrower would be left holding a key to nothing. Here preview borrows msg, then msg moves to sent, then preview is used.

fn main() {
let msg = String::from("hello");
let preview = &msg;
let sent = msg;
println!("{preview} {sent}");
}
error[E0505]: cannot move out of `msg` because it is borrowed
 --> src/main.rs:4:16
  |
2 |     let msg = String::from("hello");
  |         --- binding `msg` declared here
3 |     let preview = &msg;
  |                   ---- borrow of `msg` occurs here
4 |     let sent = msg;
  |                ^^^ move out of `msg` occurs here
5 |     println!("{preview} {sent}");
  |                ------- borrow later used here
  |
help: consider cloning the value if the performance cost is acceptable
  |
3 |     let preview = &msg.clone();
  |                       ++++++++

The usual fix. Finish with the borrow before the move: print preview first, then move msg. Or borrow again instead of moving.

fn main() {
let msg = String::from("hello");
let preview = &msg;
println!("{preview}");
let sent = msg;
println!("{sent}");
}

Link to E0505

E0507Taking a piece out of something you only borrowed

The & in &Message means the function only borrowed the message; it does not own it. Returning msg.text would carry the text away for good and leave the real owner with a message missing its text. A borrower may look at the pieces but cannot take them home.

struct Message {
text: String,
}
 
fn text_of(msg: &Message) -> String {
msg.text
}
 
fn main() {
let msg = Message { text: String::from("hello") };
println!("{}", text_of(&msg));
}
error[E0507]: cannot move out of `msg.text` which is behind a shared reference
 --> src/main.rs:6:5
  |
6 |     msg.text
  |     ^^^^^^^^ move occurs because `msg.text` has type `String`, which does not implement the `Copy` trait
  |
help: consider cloning the value if the performance cost is acceptable
  |
6 |     msg.text.clone()
  |             ++++++++

The usual fix. Hand back a borrowed view instead (-> &str and &msg.text), return a copy with msg.text.clone(), or have the function take the whole Message without the &.

struct Message {
text: String,
}
 
fn text_of(msg: &Message) -> &str {
&msg.text
}
 
fn main() {
let msg = Message { text: String::from("hello") };
println!("{}", text_of(&msg));
}

Link to E0507

E0381Reading a name before it has a value

You may make a name now and give it a value later, but it must have a value before anything reads it; Rust has no automatic "empty" or zero. Here banner only gets a value when quiet is false. On the other path it would be read while it still holds nothing.

fn main() {
let quiet = true;
let banner: &str;
if !quiet {
banner = "Welcome!";
}
println!("{banner}");
}
error[E0381]: used binding `banner` is possibly-uninitialized
 --> src/main.rs:7:16
  |
3 |     let banner: &str;
  |         ------ binding declared here but left uninitialized
4 |     if !quiet {
  |        ------ if this `if` condition is `false`, `banner` is not initialized
5 |         banner = "Welcome!";
6 |     }
  |      - an `else` arm might be missing here, initializing `banner`
7 |     println!("{banner}");
  |                ^^^^^^ `banner` used here but it is possibly-uninitialized
  |
  = note: when checking initialization, the compiler describes possible control-flow paths without evaluating whether branch conditions can actually have the values shown

The usual fix. Give it a value on every path by adding an else branch, or give it one right where it is made: let banner = if quiet { "" } else { "Welcome!" };.

fn main() {
let quiet = true;
let banner: &str;
if !quiet {
banner = "Welcome!";
} else {
banner = "";
}
println!("{banner}");
}

Link to E0381

Borrowing 2

Many readers or one changer, never both at once.

E0502Changing something while it is still being read

first borrows the first message in history: it looks at it through a reference without owning it. Then push tries to change the list while that borrow is still going to be used. Growing a list can move its items to a new place in memory, leaving first pointing at nothing, so Rust allows many readers or one changer, never both at once.

fn main() {
let mut history = vec![String::from("hi")];
let first = &history[0];
history.push(String::from("again"));
println!("{first}");
}
error[E0502]: cannot borrow `history` as mutable because it is also borrowed as immutable
 --> src/main.rs:4:5
  |
3 |     let first = &history[0];
  |                  ------- immutable borrow occurs here
4 |     history.push(String::from("again"));
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
5 |     println!("{first}");
  |                ----- immutable borrow later used here

The usual fix. Use first before you push, or take your own copy with history[0].clone() so nothing is borrowed while the list changes.

fn main() {
let mut history = vec![String::from("hi")];
let first = history[0].clone();
history.push(String::from("again"));
println!("{first}");
}

Taught in: 4.4 Borrowing

Link to E0502

E0499Two places changing the same value at once

&mut is a borrow that is allowed to change the value. Only one of those may exist at a time, so two parts of a program can never change the same thing at the same moment and trip over each other. Here a and b both hold a changing borrow of history, and both are still used.

fn main() {
let mut history: Vec<String> = Vec::new();
let a = &mut history;
let b = &mut history;
a.push(String::from("hi"));
b.push(String::from("bye"));
}
error[E0499]: cannot borrow `history` as mutable more than once at a time
 --> src/main.rs:4:13
  |
3 |     let a = &mut history;
  |             ------------ first mutable borrow occurs here
4 |     let b = &mut history;
  |             ^^^^^^^^^^^^ second mutable borrow occurs here
5 |     a.push(String::from("hi"));
  |     - first borrow later used here

The usual fix. Finish with the first &mut before taking the second: move a.push(…) above the line that makes b. Often you need no extra names at all; call history.push(…) directly.

fn main() {
let mut history: Vec<String> = Vec::new();
let a = &mut history;
a.push(String::from("hi"));
let b = &mut history;
b.push(String::from("bye"));
}

Taught in: 4.5 Borrowing to change

Link to E0499

Traits and closures 4

What a type promises it can do, and how often a closure can run.

E0277The type is missing an ability this needs

A trait is a named ability a type can have, such as being printable or comparable. This error means a value was used somewhere that needs an ability its type lacks. Beginners meet it most often by using ? (which passes an error up to whoever called the function) inside a main that does not return a Result, so there is nowhere for the error to go.

fn main() {
let count: u32 = "3".parse()?;
println!("{count}");
}
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
 --> src/main.rs:2:33
  |
1 | fn main() {
  | --------- this function should return `Result` or `Option` to accept `?`
2 |     let count: u32 = "3".parse()?;
  |                                 ^ cannot use the `?` operator in a function that returns `()`
  |
help: consider adding return type
  |
1 ~ fn main() -> Result<(), Box<dyn std::error::Error>> {
2 |     let count: u32 = "3".parse()?;
3 |     println!("{count}");
4 +     Ok(())
  |

The usual fix. Read the line naming which trait is not implemented for which type. For ? in main, make main return a Result and end it with Ok(()), as below. For an ability missing from your own struct, add it, often with #[derive(…)].

fn main() -> Result<(), std::num::ParseIntError> {
let count: u32 = "3".parse()?;
println!("{count}");
Ok(())
}

Link to E0277

E0046An impl is missing a method its trait requires

A trait is a list of methods a type promises to have; Chatterbox's Replier trait promises a reply method. An impl Replier for Echo block is where Echo keeps that promise, and it must write every method the trait lists. Here the block is empty.

trait Replier {
fn reply(&self, msg: &str) -> String;
}
 
struct Echo;
 
impl Replier for Echo {}
 
fn main() {}
error[E0046]: not all trait items implemented, missing: `reply`
 --> src/main.rs:7:1
  |
2 |     fn reply(&self, msg: &str) -> String;
  |     ------------------------------------- `reply` from trait
...
7 | impl Replier for Echo {}
  | ^^^^^^^^^^^^^^^^^^^^^ missing `reply` in implementation

The usual fix. Write each missing method inside the impl block. The error names them and shows the exact first line to copy.

trait Replier {
fn reply(&self, msg: &str) -> String;
}
 
struct Echo;
 
impl Replier for Echo {
fn reply(&self, msg: &str) -> String {
msg.to_string()
}
}
 
fn main() {
println!("{}", Echo.reply("hi"));
}

Link to E0046

E0038This trait cannot be used with dyn

dyn Replier means "some type with the Replier ability, chosen while the program runs". That only works if every method can be called without knowing the exact type, and a method that returns Self (the exact type) cannot be. The course uses dyn in the traits lesson but does not cover this rule; the idea is that such a trait still works with generics, just not behind dyn.

trait Replier {
fn duplicate(&self) -> Self;
}
 
fn main() {
let _repliers: Vec<Box<dyn Replier>> = Vec::new();
}
error[E0038]: the trait `Replier` is not dyn compatible
 --> src/main.rs:6:32
  |
6 |     let _repliers: Vec<Box<dyn Replier>> = Vec::new();
  |                                ^^^^^^^ `Replier` is not dyn compatible
  |
note: for a trait to be dyn compatible it needs to allow building a vtable
      for more information, visit <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>
 --> src/main.rs:2:28
  |
1 | trait Replier {
  |       ------- this trait is not dyn compatible...
2 |     fn duplicate(&self) -> Self;
  |                            ^^^^ ...because method `duplicate` references the `Self` type in its return type
  = help: consider moving `duplicate` to another trait

The usual fix. Remove the method that returns Self, add where Self: Sized to it so dyn leaves it out, or use generics instead of dyn.

trait Replier {
fn reply(&self, msg: &str) -> String;
}
 
fn main() {
let _repliers: Vec<Box<dyn Replier>> = Vec::new();
}

Link to E0038

E0525A closure that can run only once was asked to run again

A closure is a small unnamed function written on the spot, like || println!("hi"). If its body gives away (moves) a value it captured, it can only run once, because after the first run that value is gone. The course does not cover the kinds of closure (Fn, FnOnce); here run_twice asks for one it can call again and again, and this one gives banner away.

fn run_twice<F: Fn()>(f: F) {
f();
f();
}
 
fn main() {
let banner = String::from("Welcome!");
let show = move || {
let shown = banner;
println!("{shown}");
};
run_twice(show);
}
error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce`
  --> src/main.rs:8:16
   |
 8 |     let show = move || {
   |                ^^^^^^^ this closure implements `FnOnce`, not `Fn`
 9 |         let shown = banner;
   |                     ------ closure is `FnOnce` because it moves the variable `banner` out of its environment
...
12 |     run_twice(show);
   |     --------- ---- the requirement to implement `Fn` derives from here
   |     |
   |     required by a bound introduced by this call
   |
note: required by a bound in `run_twice`
  --> src/main.rs:1:17
   |
 1 | fn run_twice<F: Fn()>(f: F) {
   |                 ^^^^ required by this bound in `run_twice`

The usual fix. Inside the closure, borrow the value instead of moving it: println!("{banner}") only reads it. Or clone it. If one call really is enough, the function can ask for FnOnce instead.

fn run_twice<F: Fn()>(f: F) {
f();
f();
}
 
fn main() {
let banner = String::from("Welcome!");
let show = move || println!("{banner}");
run_twice(show);
}

Link to E0525

Lifetimes (beyond this course) 4

A borrow cannot outlive the value it points at.

E0597The borrowed value is gone before the borrow is

A value is cleaned up at the closing brace } of the block it was made in. Here preview borrows msg, but msg is cleaned up first, so preview would point at nothing. The course does not cover lifetimes yet; a lifetime is simply how long a value stays alive, and a borrow must never outlive its value.

fn main() {
let preview: &str;
{
let msg = String::from("hello");
preview = &msg;
}
println!("{preview}");
}
error[E0597]: `msg` does not live long enough
 --> src/main.rs:5:19
  |
4 |         let msg = String::from("hello");
  |             --- binding `msg` declared here
5 |         preview = &msg;
  |                   ^^^^ borrowed value does not live long enough
6 |     }
  |     - `msg` dropped here while still borrowed
7 |     println!("{preview}");
  |                ------- borrow later used here

The usual fix. Make the owner live longer by making msg outside the inner block, or keep an owned copy (a String) instead of a borrow.

fn main() {
let msg = String::from("hello");
let preview: &str = &msg;
println!("{preview}");
}

Link to E0597

E0106A returned borrow must say which input it comes from

This function hands back a borrow, but it was given two borrowed inputs, and Rust will not guess which one the result comes from. The course does not cover lifetimes yet; a lifetime is how long a value stays alive, and you name one with a short label like 'a to tie the result to its inputs.

fn longer(a: &str, b: &str) -> &str {
if a.len() > b.len() { a } else { b }
}
 
fn main() {
println!("{}", longer("hi", "hello"));
}
error[E0106]: missing lifetime specifier
 --> src/main.rs:1:32
  |
1 | fn longer(a: &str, b: &str) -> &str {
  |              ----     ----     ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `a` or `b`
help: consider introducing a named lifetime parameter
  |
1 | fn longer<'a>(a: &'a str, b: &'a str) -> &'a str {
  |          ++++     ++          ++          ++

The usual fix. Add a lifetime label: fn longer<'a>(a: &'a str, b: &'a str) -> &'a str. Or return an owned String, so nothing is borrowed.

fn longer<'a>(a: &'a str, b: &'a str) -> &'a str {
if a.len() > b.len() { a } else { b }
}
 
fn main() {
println!("{}", longer("hi", "hello"));
}

Taught in: 8.5 How long a borrow lasts

Link to E0106

E0515Returning a borrow of something the function throws away

Values made inside a function are cleaned up when the function ends. Returning a borrow of one would hand the caller a reference to something already gone. The course does not cover lifetimes yet; the rule here is simply that a borrow cannot outlive the value it points at.

fn shout(msg: &str) -> &str {
let loud = msg.to_uppercase();
&loud
}
 
fn main() {
println!("{}", shout("hello"));
}
error[E0515]: cannot return reference to local variable `loud`
 --> src/main.rs:3:5
  |
3 |     &loud
  |     ^^^^^ returns a reference to data owned by the current function

The usual fix. Return the value itself: change the return type to String and return loud.

fn shout(msg: &str) -> String {
let loud = msg.to_uppercase();
loud
}
 
fn main() {
println!("{}", shout("hello"));
}

Link to E0515

E0716A value with no name is cleaned up while still borrowed

String::from("Welcome") here makes a value with no name, and Rust cleans such a value up at the end of the line. banner keeps a borrow of it and is used on the next line, after it is gone. The course does not cover lifetimes yet; the fix is to give the value a name so it lives long enough.

fn main() {
let banner: &str = String::from("Welcome").as_str();
println!("{banner}");
}
error[E0716]: temporary value dropped while borrowed
 --> src/main.rs:2:24
  |
2 |     let banner: &str = String::from("Welcome").as_str();
  |                        ^^^^^^^^^^^^^^^^^^^^^^^         - temporary value is freed at the end of this statement
  |                        |
  |                        creates a temporary value which is freed while still in use
3 |     println!("{banner}");
  |                ------ borrow later used here
  |
help: consider using a `let` binding to create a longer lived value
  |
2 ~     let binding = String::from("Welcome");
3 ~     let banner: &str = binding.as_str();
  |

The usual fix. Store the value in its own variable first: let text = String::from("Welcome"); then let banner = text.as_str();.

fn main() {
let text = String::from("Welcome");
let banner: &str = text.as_str();
println!("{banner}");
}

Link to E0716

Threads and async 2

Pausing with .await needs async code, and work that may outlive the function that started it must own what it uses.

E0728.await used outside async code

.await means "pause here until this is ready, and let other work run meanwhile". Only async code can pause like that: a function marked async fn, or a block marked async { }. A plain function, including a plain main, runs straight through and has nowhere to pause. Chapter 10 covers this.

async fn reply() -> String {
String::from("hi")
}
 
fn main() {
let text = reply().await;
println!("{text}");
}
error[E0728]: `await` is only allowed inside `async` functions and blocks
 --> src/main.rs:6:24
  |
5 | fn main() {
  | --------- this is not `async`
6 |     let text = reply().await;
  |                        ^^^^^ only allowed inside `async` functions and blocks

The usual fix. Mark the function async. For main, a runtime has to drive it: with tokio, write #[tokio::main] above async fn main().

async fn reply() -> String {
String::from("hi")
}
 
fn main() {
// An async block may use .await. Nothing runs it yet: that is a runtime's job.
let _later = async {
let text = reply().await;
println!("{text}");
};
}

Taught in: 10.1 Waiting without freezing

Link to E0728

E0373A thread or task may outlive the values it borrows

A thread, or a task started with tokio::spawn, is a second line of work that runs at the same time as the rest of your program and may keep running after the function that started it has returned. Rust cannot be sure it finishes before the values it borrows are cleaned up, so it will not let it merely borrow them: it must own what it uses. Lesson 10.2 meets this.

use std::thread;
 
fn main() {
let history = vec![String::from("hi")];
let h = thread::spawn(|| println!("{}", history.len()));
h.join().unwrap();
}
error[E0373]: closure may outlive the current function, but it borrows `history`, which is owned by the current function
 --> src/main.rs:5:27
  |
5 |     let h = thread::spawn(|| println!("{}", history.len()));
  |                           ^^                ------- `history` is borrowed here
  |                           |
  |                           may outlive borrowed value `history`
  |
note: function requires argument type to outlive `'static`
 --> src/main.rs:5:13
  |
5 |     let h = thread::spawn(|| println!("{}", history.len()));
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: to force the closure to take ownership of `history` (and any other referenced variables), use the `move` keyword
  |
5 |     let h = thread::spawn(move || println!("{}", history.len()));
  |                           ++++

The usual fix. Put move before the closure, thread::spawn(move || …), or before the block, tokio::spawn(async move { … }), so the new work takes ownership of history. Clone first if you still need it here.

use std::thread;
 
fn main() {
let history = vec![String::from("hi")];
let h = thread::spawn(move || println!("{}", history.len()));
h.join().unwrap();
}

Taught in: 10.2 Tasks and channels

Link to E0373

Learn Rust by building a chat app · error atlas · compiler output from rustc 1.99.0 (b940084d7 2026-09-28) (edition 2024) · Vibe Engines · 2026