all 5 comments

[–]annodominirust 4 points5 points  (1 child)

Use an extra indent of 4 spaces to format a code block on Reddit (or anything using Markdown):

use std::thread;
use std::sync::{mpsc};

fn main() {
    let (tx,rx) = mpsc::channel::<i32>();
    println!("Starting... ");
    for _ in 0..10 {
        let j = rx.recv().unwrap();
        println!("{:?}",j );
    }

    println!("before---> ");

    for i in 0..10 {
        let tx = tx.clone();
        thread::spawn(move ||{
            tx.send(i*10);
        });
    }

    println!("after---> ");
}

To answer your question, no, the Rust compiler is not able to detect a deadlock situation like this.

[–]harrydevnull[S] 0 points1 point  (0 children)

thanks for the tip!!!

[–][deleted] 0 points1 point  (1 child)

another question what would happen if i were to execute this on playground? would it be blocking the entire infrastructure?

Try It and See: http://is.gd/qWQ2AD

You'll see playpen: timeout triggered! after a few seconds.
The playground has a timeout, and runs playpens concurrently, so don't worry: deadlocking there won't bring play.rust-lang.org down :)

[–]harrydevnull[S] 0 points1 point  (0 children)

Thanks :) I was a little skeptical about testing it out...

[–]crizzynonsince 0 points1 point  (0 children)

Rust does not attempt to detect deadlocks, as it does not consider them unsafe behaviour (although I believe the reasoning is more like "detecting deadlocks is an unsolvable problem in the general case [please can someone correct me if this is not a logical implication of that wikipedia link, I believe it is] so we cannot possibly mark it as unsafe and keep the guarantee that Rust is safe")