Skip to content

Unclear Error Message in Philosopher Example #25488

Closed
@StefanoD

Description

@StefanoD

This code...

use std::thread;

struct Philosopher {
    name: String,
}

impl Philosopher {
    fn new(name: &str) -> Philosopher {
        Philosopher {
            name: name.to_string(),
        }
    }

    fn eat(&self) {
        println!("{} is eating.", self.name);

        thread::sleep_ms(1000);

        println!("{} is done eating.", self.name);
    }
}

fn main() {
    let philosophers = vec![
        Philosopher::new("Baruch Spinoza"),
        Philosopher::new("Gilles Deleuze"),
        Philosopher::new("Karl Marx"),
        Philosopher::new("Friedrich Nietzsche"),
        Philosopher::new("Michel Foucault"),
    ];

    let handles: Vec<_> = philosophers.into_iter().map(|p| {
        thread::spawn(move || {
            p.eat();
        });
    }).collect();

    for h in handles {
        h.join().unwrap();
    }
}

... leads to:

src/main.rs:39:11: 39:17 error: type `()` does not implement any method in scope named `join`
src/main.rs:39         h.join().unwrap();
                         ^~~~~~
error: aborting due to previous error
Could not compile `Philosopher`.

Actually the error lies in line 35 in the semicolon }); at the end:

thread::spawn(move || {
            p.eat();
        });

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions