title: How do you simply read a u32 from a file?
body: <p>Alright I am very new to Rust and I'm making a modding tool port for my friend. I need to read in a gazitillion u32s from a file and I haven't found a simple solution for that. The function I came up with looks like this:</p>
  <pre><code>pub fn read_u32(filename: &amp;String, offset: usize) -&gt; u32{
      let data: Vec&lt;u8&gt; = std::fs::read(filename).unwrap();
      let sliced_data = &amp;data[offset..(offset+4)];
      let mut value: u32 = 0;
      for i in 0..4{
          value += (sliced_data[3-i] as u32) * u32::pow(256, i as u32);
      }
      return value;
  }
  </code></pre>
  <p>Now, this seems to be very, very slow. Quite obviously. How could I improve this function, or more like, with what can I replace it?</p>
  <p>(Note: The u32s all are big endian)</p>
  <p>Solution:</p>
  <pre><code>pub fn read_u32(filename: &amp;String, offset: usize) -&gt; u32{
      let mut file = std::fs::File::open(filename).unwrap();
      let mut buffer = [0; 4];
      file.seek(SeekFrom::Start(offset as u64)).unwrap();
      file.read(&amp;mut buffer[..]).unwrap() as u32;
  
      return u32::from_be_bytes(buffer);
  }
  </code></pre>
  <p>Thanks to @user1937198!</p>