{"action":"created","comment":{"url":"https://api.github.com/repos/coledot/nth/pulls/comments/33616219","id":33616219,"diff_hunk":"@@ -1,56 +1,59 @@\n use std::env;\n-use std::io::*;\n+use std::io::{stdin, Write, stderr, Error};\n use std::process::exit;\n-use std::str::FromStr;\n \n fn main() {\n   let column_numbers = parse_args();\n+  if column_numbers.is_empty() {\n+    usage_err_exit(None);\n+  }\n \n-  handle_input(&column_numbers); \n+  if let Err(err) = handle_input(&column_numbers) {\n+    writeln!(stderr(), \"Error when reading from stdin: {}\", err).ok();\n+    exit(1);\n+  }\n }\n \n fn parse_args() -> Vec<usize> {\n-  let mut arguments: Vec<_> = env::args().collect();\n-  if arguments.len() <= 1 {\n-    usage_err_exit();\n-  }\n-  arguments.remove(0);\n-  let column_numbers: Vec<usize> = arguments.iter().map(|x| {\n-    let col_arg = FromStr::from_str(x);\n-    if !col_arg.is_ok() {\n-      usage_err_exit();\n+  env::args().skip(1).map(|x| {\n+    match x.parse::<isize>() {\n+      Ok(val) if val < 1 => usage_err_exit(Some(\"Column numbers must be larger 1\".to_string())),\n+      Ok(val) => val as usize,\n+      Err(err) => usage_err_exit(Some(format!(\"Failed to parse '{}' with error: {}\", x, err))),\n     }\n-    return col_arg.unwrap();\n-  }).collect();\n-\n-  return column_numbers;\n+  }).collect()\n }\n \n-fn usage_err_exit() {\n-  println!(\"usage: nth <columns>\");\n+fn usage_err_exit(msg: Option<String>) -> ! {\n+  writeln!(stderr(), \"usage: nth <column-no> [<column-no> ...]\").ok();\n+  if let Some(msg) = msg {\n+    writeln!(stderr(), \"\\n{}\", msg).ok();\n+  }\n   exit(1);\n }\n \n-fn handle_input(column_numbers: &Vec<usize>) {\n+fn handle_input(column_numbers: &[usize]) -> Result<(), Error> {\n   let mut input = stdin();\n-  let mut read_buf: String = String::new();\n-  let mut line = input.read_line(&mut read_buf);\n-\n-  while line.is_ok() && line.unwrap() > 0 {\n-    let line_val = read_buf.clone();\n-    let column_vals: Vec<&str> = line_val.split_whitespace().collect();\n+  let mut read_buf = String::new();\n \n-    handle_line(column_vals, &column_numbers);\n+  loop {\n+    let bytes_read = try!(input.read_line(&mut read_buf));\n+    if bytes_read == 0 {\n+      return Ok(())\n+    }\n \n+      \n+    handle_line(&read_buf, &column_numbers);\n     read_buf.clear();\n-    line = input.read_line(&mut read_buf);\n   }\n }\n \n-fn handle_line(column_vals: Vec<&str>, column_numbers: &Vec<usize>) {\n+fn handle_line(read_buf: &String, column_numbers: &[usize]) {\n+  let column_vals: Vec<&str> = read_buf.split_whitespace().collect();\n   for col_num in column_numbers.iter() {\n-    if (col_num - 1) >= column_vals.len() { continue; }\n-    print!(\"{} \", column_vals[col_num - 1]);\n+    if let Some(val) = column_vals.get(col_num - 1) {\n+      print!(\"{} \", val);","path":"src/nth.rs","position":85,"original_position":85,"commit_id":"5f586bf01c987ddf643bd861f9796456f855555d","original_commit_id":"5f586bf01c987ddf643bd861f9796456f855555d","user":{"login":"killercup","id":20063,"avatar_url":"https://avatars.githubusercontent.com/u/20063?v=3","gravatar_id":"","url":"https://api.github.com/users/killercup","html_url":"https://github.com/killercup","followers_url":"https://api.github.com/users/killercup/followers","following_url":"https://api.github.com/users/killercup/following{/other_user}","gists_url":"https://api.github.com/users/killercup/gists{/gist_id}","starred_url":"https://api.github.com/users/killercup/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/killercup/subscriptions","organizations_url":"https://api.github.com/users/killercup/orgs","repos_url":"https://api.github.com/users/killercup/repos","events_url":"https://api.github.com/users/killercup/events{/privacy}","received_events_url":"https://api.github.com/users/killercup/received_events","type":"User","site_admin":false},"body":"Hey, @Byron, I just saw you struggling with the last whitespace in your video. Why don't you use `.enumerate()`, and _prepend_ a space for each but the first one? (Assuming the first one is valid.)\r\n\r\nOr, instead of `for … if let`, use `.filter_map()` on `.get()` and `.collect().connect(\" \")` (`connect` does what `join` does in Python et.al.). (This should not change the behavior of printing values immediately because printing to stdout is in fact buffered.)","created_at":"2015-06-30T19:56:10Z","updated_at":"2015-06-30T19:56:10Z","html_url":"https://github.com/coledot/nth/pull/1#discussion_r33616219","pull_request_url":"https://api.github.com/repos/coledot/nth/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/coledot/nth/pulls/comments/33616219"},"html":{"href":"https://github.com/coledot/nth/pull/1#discussion_r33616219"},"pull_request":{"href":"https://api.github.com/repos/coledot/nth/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/coledot/nth/pulls/1","id":38752375,"html_url":"https://github.com/coledot/nth/pull/1","diff_url":"https://github.com/coledot/nth/pull/1.diff","patch_url":"https://github.com/coledot/nth/pull/1.patch","issue_url":"https://api.github.com/repos/coledot/nth/issues/1","number":1,"state":"closed","locked":false,"title":"Performance enhancements and more idiomatic Rust","user":{"login":"Byron","id":63622,"avatar_url":"https://avatars.githubusercontent.com/u/63622?v=3","gravatar_id":"","url":"https://api.github.com/users/Byron","html_url":"https://github.com/Byron","followers_url":"https://api.github.com/users/Byron/followers","following_url":"https://api.github.com/users/Byron/following{/other_user}","gists_url":"https://api.github.com/users/Byron/gists{/gist_id}","starred_url":"https://api.github.com/users/Byron/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Byron/subscriptions","organizations_url":"https://api.github.com/users/Byron/orgs","repos_url":"https://api.github.com/users/Byron/repos","events_url":"https://api.github.com/users/Byron/events{/privacy}","received_events_url":"https://api.github.com/users/Byron/received_events","type":"User","site_admin":false},"body":"It was just some fun for me, please have a look and feel free to merge if you like it.\r\n\r\nGenerally there is more error checking now, which makes the code look a bit more dense in places.\r\n\r\nCheers","created_at":"2015-06-29T08:45:51Z","updated_at":"2015-06-30T19:56:10Z","closed_at":"2015-06-29T20:17:59Z","merged_at":"2015-06-29T20:17:59Z","merge_commit_sha":"a4d7f2f1fb87ba3e0cfae7f05bff2f8a999aa1e2","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/coledot/nth/pulls/1/commits","review_comments_url":"https://api.github.com/repos/coledot/nth/pulls/1/comments","review_comment_url":"https://api.github.com/repos/coledot/nth/pulls/comments{/number}","comments_url":"https://api.github.com/repos/coledot/nth/issues/1/comments","statuses_url":"https://api.github.com/repos/coledot/nth/statuses/5f586bf01c987ddf643bd861f9796456f855555d","head":{"label":"Byron:master","ref":"master","sha":"5f586bf01c987ddf643bd861f9796456f855555d","user":{"login":"Byron","id":63622,"avatar_url":"https://avatars.githubusercontent.com/u/63622?v=3","gravatar_id":"","url":"https://api.github.com/users/Byron","html_url":"https://github.com/Byron","followers_url":"https://api.github.com/users/Byron/followers","following_url":"https://api.github.com/users/Byron/following{/other_user}","gists_url":"https://api.github.com/users/Byron/gists{/gist_id}","starred_url":"https://api.github.com/users/Byron/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Byron/subscriptions","organizations_url":"https://api.github.com/users/Byron/orgs","repos_url":"https://api.github.com/users/Byron/repos","events_url":"https://api.github.com/users/Byron/events{/privacy}","received_events_url":"https://api.github.com/users/Byron/received_events","type":"User","site_admin":false},"repo":{"id":38237162,"name":"nth","full_name":"Byron/nth","owner":{"login":"Byron","id":63622,"avatar_url":"https://avatars.githubusercontent.com/u/63622?v=3","gravatar_id":"","url":"https://api.github.com/users/Byron","html_url":"https://github.com/Byron","followers_url":"https://api.github.com/users/Byron/followers","following_url":"https://api.github.com/users/Byron/following{/other_user}","gists_url":"https://api.github.com/users/Byron/gists{/gist_id}","starred_url":"https://api.github.com/users/Byron/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Byron/subscriptions","organizations_url":"https://api.github.com/users/Byron/orgs","repos_url":"https://api.github.com/users/Byron/repos","events_url":"https://api.github.com/users/Byron/events{/privacy}","received_events_url":"https://api.github.com/users/Byron/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Byron/nth","description":"a Unix command line utility to filter stdin by column numbers","fork":true,"url":"https://api.github.com/repos/Byron/nth","forks_url":"https://api.github.com/repos/Byron/nth/forks","keys_url":"https://api.github.com/repos/Byron/nth/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Byron/nth/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Byron/nth/teams","hooks_url":"https://api.github.com/repos/Byron/nth/hooks","issue_events_url":"https://api.github.com/repos/Byron/nth/issues/events{/number}","events_url":"https://api.github.com/repos/Byron/nth/events","assignees_url":"https://api.github.com/repos/Byron/nth/assignees{/user}","branches_url":"https://api.github.com/repos/Byron/nth/branches{/branch}","tags_url":"https://api.github.com/repos/Byron/nth/tags","blobs_url":"https://api.github.com/repos/Byron/nth/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Byron/nth/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Byron/nth/git/refs{/sha}","trees_url":"https://api.github.com/repos/Byron/nth/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Byron/nth/statuses/{sha}","languages_url":"https://api.github.com/repos/Byron/nth/languages","stargazers_url":"https://api.github.com/repos/Byron/nth/stargazers","contributors_url":"https://api.github.com/repos/Byron/nth/contributors","subscribers_url":"https://api.github.com/repos/Byron/nth/subscribers","subscription_url":"https://api.github.com/repos/Byron/nth/subscription","commits_url":"https://api.github.com/repos/Byron/nth/commits{/sha}","git_commits_url":"https://api.github.com/repos/Byron/nth/git/commits{/sha}","comments_url":"https://api.github.com/repos/Byron/nth/comments{/number}","issue_comment_url":"https://api.github.com/repos/Byron/nth/issues/comments{/number}","contents_url":"https://api.github.com/repos/Byron/nth/contents/{+path}","compare_url":"https://api.github.com/repos/Byron/nth/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Byron/nth/merges","archive_url":"https://api.github.com/repos/Byron/nth/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Byron/nth/downloads","issues_url":"https://api.github.com/repos/Byron/nth/issues{/number}","pulls_url":"https://api.github.com/repos/Byron/nth/pulls{/number}","milestones_url":"https://api.github.com/repos/Byron/nth/milestones{/number}","notifications_url":"https://api.github.com/repos/Byron/nth/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Byron/nth/labels{/name}","releases_url":"https://api.github.com/repos/Byron/nth/releases{/id}","created_at":"2015-06-29T08:44:12Z","updated_at":"2015-06-29T08:44:13Z","pushed_at":"2015-06-29T09:00:58Z","git_url":"git://github.com/Byron/nth.git","ssh_url":"git@github.com:Byron/nth.git","clone_url":"https://github.com/Byron/nth.git","svn_url":"https://github.com/Byron/nth","homepage":"","size":0,"stargazers_count":0,"watchers_count":0,"language":"Rust","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"coledot:master","ref":"master","sha":"aa74ff19b76622f0711991e107bbefe60f3864d1","user":{"login":"coledot","id":3317498,"avatar_url":"https://avatars.githubusercontent.com/u/3317498?v=3","gravatar_id":"","url":"https://api.github.com/users/coledot","html_url":"https://github.com/coledot","followers_url":"https://api.github.com/users/coledot/followers","following_url":"https://api.github.com/users/coledot/following{/other_user}","gists_url":"https://api.github.com/users/coledot/gists{/gist_id}","starred_url":"https://api.github.com/users/coledot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coledot/subscriptions","organizations_url":"https://api.github.com/users/coledot/orgs","repos_url":"https://api.github.com/users/coledot/repos","events_url":"https://api.github.com/users/coledot/events{/privacy}","received_events_url":"https://api.github.com/users/coledot/received_events","type":"User","site_admin":false},"repo":{"id":38220044,"name":"nth","full_name":"coledot/nth","owner":{"login":"coledot","id":3317498,"avatar_url":"https://avatars.githubusercontent.com/u/3317498?v=3","gravatar_id":"","url":"https://api.github.com/users/coledot","html_url":"https://github.com/coledot","followers_url":"https://api.github.com/users/coledot/followers","following_url":"https://api.github.com/users/coledot/following{/other_user}","gists_url":"https://api.github.com/users/coledot/gists{/gist_id}","starred_url":"https://api.github.com/users/coledot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coledot/subscriptions","organizations_url":"https://api.github.com/users/coledot/orgs","repos_url":"https://api.github.com/users/coledot/repos","events_url":"https://api.github.com/users/coledot/events{/privacy}","received_events_url":"https://api.github.com/users/coledot/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/coledot/nth","description":"a Unix command line utility to filter stdin by column numbers","fork":false,"url":"https://api.github.com/repos/coledot/nth","forks_url":"https://api.github.com/repos/coledot/nth/forks","keys_url":"https://api.github.com/repos/coledot/nth/keys{/key_id}","collaborators_url":"https://api.github.com/repos/coledot/nth/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/coledot/nth/teams","hooks_url":"https://api.github.com/repos/coledot/nth/hooks","issue_events_url":"https://api.github.com/repos/coledot/nth/issues/events{/number}","events_url":"https://api.github.com/repos/coledot/nth/events","assignees_url":"https://api.github.com/repos/coledot/nth/assignees{/user}","branches_url":"https://api.github.com/repos/coledot/nth/branches{/branch}","tags_url":"https://api.github.com/repos/coledot/nth/tags","blobs_url":"https://api.github.com/repos/coledot/nth/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/coledot/nth/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/coledot/nth/git/refs{/sha}","trees_url":"https://api.github.com/repos/coledot/nth/git/trees{/sha}","statuses_url":"https://api.github.com/repos/coledot/nth/statuses/{sha}","languages_url":"https://api.github.com/repos/coledot/nth/languages","stargazers_url":"https://api.github.com/repos/coledot/nth/stargazers","contributors_url":"https://api.github.com/repos/coledot/nth/contributors","subscribers_url":"https://api.github.com/repos/coledot/nth/subscribers","subscription_url":"https://api.github.com/repos/coledot/nth/subscription","commits_url":"https://api.github.com/repos/coledot/nth/commits{/sha}","git_commits_url":"https://api.github.com/repos/coledot/nth/git/commits{/sha}","comments_url":"https://api.github.com/repos/coledot/nth/comments{/number}","issue_comment_url":"https://api.github.com/repos/coledot/nth/issues/comments{/number}","contents_url":"https://api.github.com/repos/coledot/nth/contents/{+path}","compare_url":"https://api.github.com/repos/coledot/nth/compare/{base}...{head}","merges_url":"https://api.github.com/repos/coledot/nth/merges","archive_url":"https://api.github.com/repos/coledot/nth/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/coledot/nth/downloads","issues_url":"https://api.github.com/repos/coledot/nth/issues{/number}","pulls_url":"https://api.github.com/repos/coledot/nth/pulls{/number}","milestones_url":"https://api.github.com/repos/coledot/nth/milestones{/number}","notifications_url":"https://api.github.com/repos/coledot/nth/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/coledot/nth/labels{/name}","releases_url":"https://api.github.com/repos/coledot/nth/releases{/id}","created_at":"2015-06-29T00:45:35Z","updated_at":"2015-06-30T10:05:13Z","pushed_at":"2015-06-30T01:56:19Z","git_url":"git://github.com/coledot/nth.git","ssh_url":"git@github.com:coledot/nth.git","clone_url":"https://github.com/coledot/nth.git","svn_url":"https://github.com/coledot/nth","homepage":"","size":0,"stargazers_count":3,"watchers_count":3,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":0,"forks":2,"open_issues":0,"watchers":3,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/coledot/nth/pulls/1"},"html":{"href":"https://github.com/coledot/nth/pull/1"},"issue":{"href":"https://api.github.com/repos/coledot/nth/issues/1"},"comments":{"href":"https://api.github.com/repos/coledot/nth/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/coledot/nth/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/coledot/nth/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/coledot/nth/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/coledot/nth/statuses/5f586bf01c987ddf643bd861f9796456f855555d"}}}}