2016-11-13 64 views
0

受evilone在帖子How to print a Vec?中提供的代碼的啓發。要顯示一個矩陣,我寫的代碼如下:使用for循環顯示矩陣時的不匹配類型

use std::{ops, fmt}; 

#[derive(PartialEq, Debug)] 
pub struct Matrix<T> { 
    data: Vec<T>, 
    row: usize, 
    col: usize, 
} 

impl<T: Copy> Matrix<T> {  
    pub fn new(row: usize, col: usize, values: &[T]) -> Matrix<T> { 
     Matrix { 
      data: values.to_vec(), 
      row: row, 
      col: col, 
     } 
    } 
}  

//// Display 
impl<T: fmt::Display> fmt::Display for Matrix<T> { 

    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 
     let n_row = self.row; 
     let n_col = self.col; 
     let data = self.data; 

     for i in 0.. n_row { 
      let mut each_row = String::new(); 

      for j in 0.. n_col { 
       let idx = i * n_col + j; 
       let each_element = data[idx]; 
       each_row.push_str(&each_element.to_string()); 
       each_row.push_str(" "); // seperated by space 
      } 
      write!(f, "{}", each_row) 
     } 
    } 
}  

fn main() { 
    let x = Matrix::new(2, 3, &[-6, -5, 0, 1, 2, 3]); 
    println!("{}", x); 

} 

我得到的錯誤:

rustc 1.13.0 (2c6933acc 2016-11-07) 
error[E0308]: mismatched types 
    --> <anon>:40:13 
    | 
40 |    write!(f, "{}", each_row) 
    |    ^^^^^^^^^^^^^^^^^^^^^^^^^ expected(), found enum `std::result::Result` 
    | 
    = note: expected type `()` 
    = note: found type `std::result::Result<(), std::fmt::Error>` 
    = note: this error originates in a macro outside of the current crate 

error[E0308]: mismatched types 
    --> <anon>:31:9 
    | 
31 |   for i in 0.. n_row { 
    |  ^expected enum `std::result::Result`, found() 
    | 
    = note: expected type `std::result::Result<(), std::fmt::Error>` 
    = note: found type `()` 

1)我不明白爲什麼我得到expected(), found enum `std::result::Result`

2)對於第二錯誤,我認爲這是由於沒有實現第40行而引起的。因此,如果修復第40行,這將不再是問題。

任何建議來解決這個問題?

回答

2

編程時,創建一個Minimal, Complete, and Verifiable Example是有用的。這意味着您可以刪除與您遇到的錯誤或問題無關的所有內容,並將其煮至純粹的本質。一般來說,你會發現這樣做會大大縮小問題的潛在位置。通常,這可以讓你自己回答問題,而其他時候它可以增加提問的機會。

下面是一個MCVE對於這個問題:

fn foo() -> u8 { 
    for i in 0..1u8 { 
     i 
    } 
} 

fn main() {} 

非常小,是不是?它會產生這些錯誤:

error[E0308]: mismatched types 
--> src/main.rs:3:9 
    | 
3 |   i 
    |  ^expected(), found u8 
    | 
    = note: expected type `()` 
    = note: found type `u8` 

error[E0308]: mismatched types 
--> src/main.rs:2:5 
    | 
2 |  for i in 0..1u8 { 
    | ^expected u8, found() 
    | 
    = note: expected type `u8` 
    = note: found type `()` 

這應該看起來很熟悉。

現在您可以問自己的問題:for循環評估什麼類型和值? (提示:編譯器的消息實際上是告訴你,如果你看過他們以正確的方式)

我們知道函數必須返回一個u8,但是編譯器告訴我們,我們實際上是返回一個() - 這是第二個錯誤。這意味着for循環的計算結果爲()!由於for循環的計算結果爲(),因此for循環塊的計算結果可能會發生什麼變化?正如你可以猜到的,答案是不能返回一個值!

想想這個例如:

fn foo() -> u8 { 
    for i in 0..0u8 { 
     // 
    } 
} 

會是什麼回報?是的,可能沒什麼好。


從我們MCVE返回回到原來的問題,你需要明確地返回內部故障,並明確在循環的最後返回成功:

for /* ... */ { 
    // ... 

    try!(write!(f, "{}", each_row)) 
} 

Ok(()) 

這只是打開門其他錯誤在代碼中,但你有能力搞清楚這些!

+0

非常感謝您指導我看到本質。我完全同意我應該學會如何深入瞭解錯誤報告並製作MCVE案例。這些日子裏,我在魯斯特掙扎和學習很多。 – enaJ

+1

@enaJ繼續努力!製作一個重複出現錯誤的簡化案例可以幫助您在整個編程生涯中發揮作用。 – Shepmaster

+0

絕對!我來自蟒蛇背景,6周前開始學習Rust。他們在很多方面都有很大的不同。但我確實認爲每種語言都有助於更深入地瞭解另一種語言。 – enaJ