2016-08-16 2 views
1

Ich versuche in Rust mit dem Multithreading herumzuspielen und bin auf etwas gestoßen, das ich eher trivial finde. Hier ist der Code-Schnipsel:Warum kann ich es nicht versuchen! Mutex.lock?

use std::thread; 
use std::sync::{Arc, Mutex}; 

fn main() { 
    let vec: Vec<i32> = vec!(1, 2, 3); 
    let shared = Arc::new(Mutex::new(vec)); 

    let clone = shared.clone(); 

    let join_handle = thread::spawn(move || { 
     let mut data = clone.lock().unwrap(); 
     data.push(5); 
    }); 

    join_handle.join().unwrap(); 

    let clone = shared.clone(); 
    let vec = try!(clone.lock()); 

    println!("{:?}", *vec); 
} 

Also, meine Frage ist auf der Linie let vec = try!(clone.lock()). Dies verursacht den folgenden Compiler-Fehler:

error: mismatched types [E0308] 
return $ crate :: result :: Result :: Err (
    ^
note: in this expansion of try! (defined in <std macros>) 
help: run `rustc --explain E0308` to see a detailed explanation 
note: expected type `()` 
note: found type `std::result::Result<_, _>` 

Für mich macht dies nicht viel Sinn. clone.lock() gibt TryLockResult<MutexGuard<T>> zurück, was im Wesentlichen in Result<MutexGuard<T>, PoisonedError<MutexGuard<T>> übersetzt wird, was bedeutet, dass try!(clone.lock()) entweder zu einem Wurf oder MutexGuard<T> aufgelöst werden sollte.

Verstehe ich hier etwas grundsätzlich falsch?

Antwort

2

Das Ausführen des 'Erklären' auf Rustc wird diesen Fehler erklären - ich dachte, ich hätte es schon, aber ich denke nicht.

This error occurs when the compiler was unable to infer the concrete type of a 
variable. It can occur for several cases, the most common of which is a 
mismatch in the expected type that the compiler inferred for a variable's 
initializing expression, and the actual type explicitly assigned to the 
variable. 

For example: 

``` 
let x: i32 = "I am not a number!"; 
//  ~~~ ~~~~~~~~~~~~~~~~~~~~ 
//  |    | 
//  | initializing expression; 
//  | compiler infers type `&str` 
//  | 
// type `i32` assigned to variable `x` 
``` 

Another situation in which this occurs is when you attempt to use the `try!` 
macro inside a function that does not return a `Result<T, E>`: 

``` 
use std::fs::File; 

fn main() { 
    let mut f = try!(File::create("foo.txt")); 
} 
``` 

This code gives an error like this: 

``` 
<std macros>:5:8: 6:42 error: mismatched types: 
expected `()`, 
    found `core::result::Result<_, _>` 
(expected(), 
    found enum `core::result::Result`) [E0308] 
``` 

`try!` returns a `Result<T, E>`, and so the function must. But `main()` has 
`()` as its return type, hence the error. 

Mit anderen Worten, ist der Fehler nicht mit der Art der Mutex; es ist, weil ich try! innerhalb der Hauptfunktion verwende; try! erzwingt die umschließende Funktion geben Sie zurück, aber die Hauptfunktion muss () zurückgeben.

Wenn wir uns den erweiterten Code aussehen:

let vec = 
    match clone.lock() { 
     ::std::result::Result::Ok(val) => val, 
     ::std::result::Result::Err(err) => { 
      return ::std::result::Result::Err(::std::convert::From::from(err)) 
     } 
    }; 

Der Grund, warum try! schreit dich ist, weil die return Anweisung an den Block gilt nicht in match, es gilt für die einschließende Methode.

Verwandte Themen