rust - Mismatched types when displaying a matrix with a for loop -


inspired code provided evilone in post how print vec?. display matrix, wrote code following:

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 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;          in 0.. n_row {             let mut each_row = string::new();               j in 0.. n_col {                 let idx = * n_col + j;                   let each_element = data[idx];                 each_row.push_str(&each_element.to_string());                 each_row.push_str(" ");  // seperated space              }             write!(f, "{}", each_row)            }     } }      fn main() {     let x = matrix::new(2, 3, &[-6, -5, 0, 1, 2, 3]);     println!("{}", x);  } 

i got errors:

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: error originates in macro outside of current crate  error[e0308]: mismatched types   --> <anon>:31:9    | 31 |         in 0.. n_row {    |         ^ expected enum `std::result::result`, found ()    |    = note: expected type `std::result::result<(), std::fmt::error>`    = note:    found type `()` 

1) don't understand why expected (), found enum `std::result::result`

2) second error, thought caused failure implement line 40. if fix line 40, won't problem anymore.

any suggestions fix this?

when programming, it's useful create minimal, complete, , verifiable example. means remove that's not relevant error or problem having, boiling down pure essence. generally, find doing narrows down potential location of problem. often, allows answer question yourself, , other times increases chances of being able ask insightful question.

here's mcve problem:

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

pretty small, isn't it? produces these errors:

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

which should familiar.

now question can ask yourself: type , value for loop evaluate to? (hint: compiler messages tell if read them right way)

we know function must return u8, compiler tells returning () — second error. means for loop evaluates ()! since for loop evaluates (), possibly happen value for loop's block evaluates to? can guess, answer block cannot return value!

think this example:

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

what this return? yeah, nothing good.


returning our mcve original problem, need explicitly return inner failures , explicitly return success @ end of loop:

for /* ... */ {     // ...      try!(write!(f, "{}", each_row)) }  ok(()) 

this opens door other errors in code, capable of figuring out!


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -