Skip to content

Commit 017ebf5

Browse files
committed
Improved example [skip ci]
1 parent 6b41d7e commit 017ebf5

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

examples/disco/src/main.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use postgres::{Client, NoTls};
44
use std::collections::HashMap;
55
use std::error::Error;
66
use std::fs::File;
7-
use std::io::{BufRead, BufReader, Read};
7+
use std::io::{BufRead, BufReader};
88
use std::path::Path;
99

1010
fn main() -> Result<(), Box<dyn Error>> {
@@ -68,17 +68,19 @@ fn main() -> Result<(), Box<dyn Error>> {
6868
fn load_movielens(path: &Path) -> Result<Dataset<i32, String>, Box<dyn Error>> {
6969
// read movies, removing invalid UTF-8 bytes
7070
let mut movies = HashMap::with_capacity(2000);
71-
let mut movies_file = File::open(path.join("u.item"))?;
71+
let movies_file = File::open(path.join("u.item"))?;
72+
let mut rdr = BufReader::new(movies_file);
7273
let mut buf = Vec::new();
73-
movies_file.read_to_end(&mut buf)?;
74-
let movies_data = String::from_utf8_lossy(&buf);
75-
let rdr = BufReader::new(movies_data.as_bytes());
76-
for line in rdr.lines() {
77-
let line = line?;
74+
while let Ok(len) = rdr.read_until(b'\n', &mut buf) {
75+
if len == 0 {
76+
break;
77+
}
78+
let line = String::from_utf8_lossy(&buf);
7879
let mut row = line.split('|');
7980
let id = row.next().unwrap().to_string();
8081
let name = row.next().unwrap().to_string();
8182
movies.insert(id, name);
83+
buf.clear();
8284
}
8385

8486
// read ratings and create dataset

0 commit comments

Comments
 (0)