Skip to content

Instantly share code, notes, and snippets.

@marcomorain
Last active January 2, 2016 20:38
Show Gist options
  • Select an option

  • Save marcomorain/8357744 to your computer and use it in GitHub Desktop.

Select an option

Save marcomorain/8357744 to your computer and use it in GitHub Desktop.
I tried to use Rust today....

Rust

My first idea: clone the comm commandline tool.

use std::io::buffered::BufferedReader;
use std::io::stdin;
use std::os;
use std::io::File;

fn lines(path: ~str) -> ~[~str] {
  return BufferedReader::new(File::open(&Path::new(path))).lines().collect()
}

fn main() {
  let args : ~[~str] = os::args();

  let mut a = lines(args[0]);
  let mut b = lines(args[1]);
}

This produces

main.rs:7:10: 7:60 error: borrowed value does not live long enough
main.rs:7   return BufferedReader::new(File::open(&Path::new(path))).lines().collect()
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.rs:7:10: 8:2 note: reference must be valid for the method call at 7:9...
main.rs:7   return BufferedReader::new(File::open(&Path::new(path))).lines().collect()
main.rs:8 }
main.rs:7:10: 7:68 note: ...but borrowed value is only valid for the method call at 7:9
main.rs:7   return BufferedReader::new(File::open(&Path::new(path))).lines().collect()
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.rs:14:21: 14:25 error: use of partially moved value: `args`
main.rs:14   let mut b = lines(args[1]);
                               ^~~~
main.rs:13:21: 13:27 note: `(*args)[]` moved here because it has type `~str`, which is non-copyable (perhaps you meant to use clone()?)
main.rs:13   let mut a = lines(args[0]);
                               ^~~~~~
error: aborting due to 2 previous errors
task 'rustc' failed at 'explicit failure', /private/tmp/rust-R5p2/rust-0.9/src/libsyntax/diagnostic.rs:75
task '<main>' failed at 'explicit failure', /private/tmp/rust-R5p2/rust-0.9/src/librustc/lib.rs:453

Here is my second attempt after some help from @pcwalton

use std::io::buffered::BufferedReader;
use std::io::stdin;
use std::os;
use std::io::File;

fn lines(path: ~str) -> ~[~str] {
  let mut buffer = BufferedReader::new(File::open(&Path::new(path)));
  let mut lines = buffer.lines();
  return lines.collect();
}

fn main() {
  let args : ~[~str] = os::args();

  let mut a = lines(args[0].clone());
  let mut b = lines(args[1].clone());
}

This compiles and links, and then I get a runtime error.

marc@astrotrain ~/dev/rust $ rustc main.rs
marc@astrotrain ~/dev/rust $ ./main main.rs main.rs
task '<main>' failed at 'Unhandled condition: not_utf8: ~"from_utf8: input is not UTF-8; first bad byte is 207"', /private/tmp/rust-R5p2/rust-0.9/src/libstd/condition.rs:139
marc@astrotrain ~/dev/rust $

I'm now searching for how to enable debug symbols to get a backtrace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment