I just started learning Rust, and I'm making some small tools to help me understand the language. I have a problem formatting a String
using the format!
macro. As format!
takes a literal, I am not able pass my string to it. I want to do this to dynamically add strings into the current string for use in a view engine. I'm open for suggestions if there might be a better way to do it.
let test = String::from("Test: {}");
let test2 = String::from("Not working!");
println!(test, test2);
What I actually want to achieve is the below example, where main.html contains {content}
.
use std::io::prelude::*;
use std::fs::File;
use std::io;
fn main() {
let mut buffer = String::new();
read_from_file_using_try(&mut buffer);
println!(&buffer, content="content");
}
fn read_from_file_using_try(buffer: &mut String) -> Result<(), io::Error> {
let mut file = try!(File::open("main.html"));
try!(file.read_to_string(buffer));
Ok(())
}
So I want to print the contents of main.html after I formatted it.