0

I'm trying to read a binary file in Rust and output the hex values of it on a file. I got most of the code working but got stuck on the "write" part:

use std::io::prelude::*;
use std::fs::File;

fn main() {
    let f = File::open("input");
    match f {
        Ok(f) => {
            let mut s = String::new();
            for byte in f.bytes() {
                s = s + &format!("{:02X} ", byte.unwrap());
            }

            let mut buffer = File::create("out.txt").unwrap();
            buffer.write(s);
        },
        Err(_) => println!("Bad") // ignoring bindings
    }

}

I am trying to write s of type std::string::String while buffer.write() will only accept a primitive string of type &[u8]. I've been scouring the docs for a while now to no avail so I decided to ask here.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
kosinix
  • 1,779
  • 1
  • 18
  • 22
  • @Shepmaster This question is mostly about writing, but the linked question and its answers despite its title are about reading, so I'm not sure whether it should be marked as duplicate. – krdln Sep 11 '16 at 01:54
  • @krdln indeed. I updated the answer there to include writing (as it should always have due to the name) – Shepmaster Sep 11 '16 at 01:57
  • 1
    @kosinix The best way to write formatted text to a file (or anything which is `Write`) would be to use the [`write!` and `writeln!` macros](https://static.rust-lang.org/doc/master/std/macro.write.html). Make sure you wrap the file in a `BufWriter` though if you're making a lot of small writes. – krdln Sep 11 '16 at 01:58
  • Another [question about the `write!` / `writeln!` macros](http://stackoverflow.com/q/32472495/155423). – Shepmaster Sep 11 '16 at 02:07
  • @Shepmaster this is a different question from the one you linked to. Please remove the duplicate tag. Somebody has edited the title to better convey the question. Also, notice that your answer is writing string that is already in &[u8] as oppose to std::string::String. – kosinix Sep 11 '16 at 02:30
  • @kosinix [I edited the title](http://stackoverflow.com/posts/39431998/revisions) :-) Please double check the linked duplicate; notice that it specifically says *You can convert a `String` / `&str` to bytes with `as_bytes`*. That's the answer you need. – Shepmaster Sep 11 '16 at 02:47
  • @krdln - if you could turn it into an answer with code and just a bit of explanation i'd be glad to accept it. – kosinix Sep 11 '16 at 02:49
  • I don't believe that new answers can be added to a closed question. Others that disagree with my opinion that this is a duplicate can vote to reopen it (you are unfortunately just under the [250 reputation needed to vote on it yourself](http://stackoverflow.com/help/privileges)). – Shepmaster Sep 11 '16 at 02:59
  • @Shepmaster How can this be an "exact duplicate" question since this is about converting the String type to primitive string? You yourself edited the title to convey that. Your linked answer, while detailed and thorough, does not have emphasis on converting to and from these two types. The two string types are going to be a source of confusion in the future. (PS. You dont have to be a smug about my lowly points. I dont have the habit of channeling points to my answers by marking other questions duplicate.) :-) – kosinix Sep 11 '16 at 03:36
  • @kosinix it's an exact duplicate because the code is *exactly* the same. Call `as_bytes` on `String` or `&str` to pass it to `write` like the other answer says. I don't know what you mean by "primitive string" other than `&str`. I'm sorry that I've given you the impression that I am trying to be smug; that was not my intention. Perhaps I should disengage before giving you a worse opinion of myself. – Shepmaster Sep 11 '16 at 03:48

0 Answers0