title: Read Json in Rust
body: <p>I am trying to read the contents of a Json file in Rust. This file contains an array of points in the plane, like <code>[[1, 2], [3.5, 2.7], [0, -2.1]]</code>.</p>
  
  <p>My first attempt at an implementation is</p>
  
  <pre><code>extern crate serialize;
  use serialize::json;
  use std::io::File;
  
  fn read_points() -&gt; Vec&lt;[f64, ..2]&gt; {
    let contents = File::open(&amp;Path::new("../points.json")).read_to_string().unwrap();
    let points: Vec&lt;Vec&lt;f64&gt;&gt; = json::decode(contents.as_slice()).unwrap();
  
    points.iter().map(|&amp;v| [v[0], v[1]]).collect::&lt;Vec&lt;[f64, ..2]&gt;&gt;()
  }
  </code></pre>
  
  <p>Now I have two problems with this.</p>
  
  <p>First, I get a compile error</p>
  
  <pre><code>error: cannot move out of dereference of `&amp;`-pointer
  </code></pre>
  
  <p>which seems to imply that my map operation is not safe. Being a complete newbie to Rust, it is not obvious to me where <code>&amp;v</code> resides in memory. Ideally, I would like to access the underlying array backing the <code>Vec&lt;f64&gt;</code>, or even better, avoid to allocate the inner vectors an read directly the json to a <code>Vec&lt;[f64, ..2]&gt;</code>.</p>
  
  <p>Second - but less important - there are those two ugly <code>unwrap</code> calls. Now, I understand that both reading a file and parsing json may fail. Is there a way to easily combine <code>Result</code> instances, such as <code>flatmap</code> in Scala or <code>bind</code> in Haskell? Even better, something like the do notation?</p>