0

My function is as follows:

fn read_file () -> String {
  let mut file = File::open("./assets/postdata/lorem_ipsum.md").unwrap();
  let mut buffer = Vec::new();
  let read_variable = file.read_to_end(&mut buffer).unwrap();
  let filestr = String::from_utf8(buffer).unwrap();

  return filestr;
}

The file is a markdown file with four paragraphs of lorem ipsum text. This function, when called by a handlebars record generator, prints to a webpage producing a wall of text. I'm obviously missing something; how can I make the Rust compiler recognise the whitespace?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
NecroTechno
  • 301
  • 1
  • 2
  • 9
  • This sounds like it's unrelated to Rust or Iron: if all you're doing is dumping Markdown into HTML, then paragraph breaks won't show because HTML doesn't interpret newlines (outside of preformatted text elements). `read_to_end` absolutely does not "ignore" whitespace or anything else. – DK. Oct 28 '15 at 09:42
  • I tried adding to each newline, but all it did was show the tags. Is there another workaround? – NecroTechno Oct 28 '15 at 09:48
  • Did you set the `Content-Type` header to `text/html`? – Francis Gagné Oct 28 '15 at 10:54
  • Have you tried replacing `read_to_end` with a hard coded chunk of data? Something like `fn read_file() -> String { "hello\nworld\nI\nam\ntext".into() }`. You might as well use `read_to_string` as it combines two of your lines. `read_to_end` isn't part of the Rust *compiler*. This is just part of the *standard library*. – Shepmaster Oct 28 '15 at 15:58
  • It was actually an issue with Handlebars-rust and not interpreting the string correctly. Thanks everyone for your suggestions. – NecroTechno Oct 31 '15 at 04:04

0 Answers0