Reputation: 1655
Inspired by the code provided by evilone in the post How to print a Vec?. To display a matrix, I wrote code as 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 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);
}
I got the 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: 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) I don't understand why I get expected (), found enum `std::result::Result`
2) For the second error, I thought it was caused by the failure to implement line 40. So if fix line 40, it won't be a problem anymore.
Any suggestions to fix this?
Upvotes: 0
Views: 891
Reputation: 432089
Here's an MRE for this problem:
fn foo() -> u8 {
for i in 0..1u8 {
i
}
}
It produces these familiar errors:
error[E0308]: mismatched types
--> src/lib.rs:3:9
|
3 | i
| ^ expected `()`, found `u8`
|
help: you might have meant to return this value
|
3 | return i;
| ++++++ +
error[E0308]: mismatched types
--> src/lib.rs:2:5
|
1 | fn foo() -> u8 {
| -- expected `u8` because of return type
2 | / for i in 0..1u8 {
3 | | i
4 | | }
| |_____^ expected `u8`, found `()`
Now the question is "what type and value does a for
loop evaluate to?" (Hint: the compiler messages actually tell you if you read them the right way)
We know that the function must return a u8
, but the compiler tells us that we are actually returning a ()
— this is the second error. That means that a for
loop evaluates to ()
! Since a for
loop evaluates to ()
, what could possibly happen to the value that the for
loop's block evaluates to? As you can guess, the answer is that the block cannot return a value!
Think about this example:
fn foo() -> u8 {
for i in 0..0u8 {
//
}
}
What would this return? Probably nothing good.
Returning from our MRE back to the original problem, you need to explicitly return inner failures and explicitly return success after the end of the loop:
for /* ... */ {
// ...
write!(f, "{}", each_row)?;
}
Ok(())
This opens the door to other errors in the code, but I believe you are capable of figuring those out!
Upvotes: 3