2016-05-12 8 views
3

私はRust by Example websiteに "タプルレッスン"を渡そうとしていますが、フォーマットされた出力実装に取り​​残されています。フォーマットされた出力の値として文字列を取得します

#[derive(Debug)] 
struct Matrix{ 
    data: Vec<Vec<f64>> // [[...], [...],] 
} 

impl fmt::Display for Matrix { 
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 
     let output_data = self.data 
      // [[1, 2], [2, 3]] -> ["1, 2", "2, 3"] 
      .into_iter() 
      .map(|row| { 
       row.into_iter() 
        .map(|value| value.to_string()) 
        .collect::<Vec<String>>() 
        .join(", ") 
      }) 
      .collect::<Vec<String>>() 
      // ["1, 2", "2, 3"] -> ["(1, 2)", "(2, 3)"] 
      .into_iter() 
      .map(|string_row| { format!("({})", string_row) }) 
      // ["(1, 2)", "(2, 3)"] -> "(1, 2),\n(2, 3)" 
      .collect::<Vec<String>>() 
      .join(",\n"); 
     write!(f, "{}", output_data) 
    } 
} 

しかし、コンパイラは、次のメッセージ出力します:私はRefCelloutput_dataの結果をラップしようとした

<anon>:21:40: 21:44 error: cannot move out of borrowed content [E0507] 
<anon>:21   let output_data = self.data 
            ^~~~ 
<anon>:21:40: 21:44 help: see the detailed explanation for E0507 
error: aborting due to previous error 
playpen: application terminated with error code 101 

を、しかし、コンパイラ私は、渡された行列を出力し、このコードを、持っていますこのエラーは依然として表示されます。 write!マクロが正しく動作するように、この問題を解決するにはどうすればよいですか?

答えて

5

問題は、それは、selfからdataを外に移動されている、それは(それがエラーが言うことである)許可されていない、into_interdataの所有権を取ることです。所有権を取得せずに、ベクターに反復処理するには、iterメソッドを使用します。

fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 
    let output_data = self.data 
     // [[1, 2], [2, 3]] -> ["1, 2", "2, 3"] 
     .iter() 
     .map(|row| { 
      row.into_iter() 
       .map(|value| value.to_string()) 
       .collect::<Vec<String>>() 
       .join(", ") 
     }) 
     .collect::<Vec<String>>() 
     // ["1, 2", "2, 3"] -> ["(1, 2)", "(2, 3)"] 
     .into_iter() 
     .map(|string_row| { format!("({})", string_row) }) 
     // ["(1, 2)", "(2, 3)"] -> "(1, 2),\n(2, 3)" 
     .collect::<Vec<String>>() 
     .join(",\n"); 
    write!(f, "{}", output_data) 
} 

Formatterを見てみましょう。それはfmtを書くのを助けるいくつかの方法があります。仲介ベクトルと文字列を割り当てないバージョンがあります:

fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 
    let mut sep = ""; 
    for line in &self.data { // this is like: for line in self.data.iter() 
     try!(f.write_str(sep)); 
     let mut d = f.debug_tuple(""); 
     for row in line { 
      d.field(row); 
     } 
     try!(d.finish()); 
     sep = ",\n"; 
    } 
    Ok(()) 
} 
関連する問題