TODO test
(fname: &str)
| 280 | |
| 281 | // TODO test |
| 282 | pub fn from_file<R: FromStr + Clone>(fname: &str) -> Option<Matrix<R>> { |
| 283 | |
| 284 | match fs::File::open(fname) { |
| 285 | Ok(mut f) => { |
| 286 | let mut data = String::new(); |
| 287 | match f.read_to_string(&mut data) { |
| 288 | Ok(_n) => { |
| 289 | let mut m: Matrix<R> = Matrix::new(); |
| 290 | for line in data.split("\n") { |
| 291 | let items = |
| 292 | line.split(|c: char| c.is_whitespace()).filter(|s| s.len() > 0); |
| 293 | let mut v: Vec<R> = Vec::new(); |
| 294 | for s in items { |
| 295 | match s.parse::<R>() { |
| 296 | Ok(val) => v.push(val), |
| 297 | _ => { return None; } |
| 298 | } |
| 299 | } |
| 300 | if v.len() > 0 { |
| 301 | if m.rows() > 0 && v.len() != m.cols() { |
| 302 | return None; |
| 303 | } |
| 304 | m.add_row(&v); |
| 305 | } |
| 306 | } |
| 307 | Some(m) |
| 308 | } |
| 309 | _ => None |
| 310 | } |
| 311 | } |
| 312 | _ => None |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /// Returns `true` if matrix has no rows and no columns, i.e. |
| 317 | /// the matrix does not contain an element. |