Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a dynamic format string with the format! macro?

I want to use the format! macro with a String as first argument, but because the macro expects a string literal, I am not able pass anything different 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.

like image 349
Sune Avatar asked Sep 14 '15 19:09

Sune


People also ask

What is string format () and how can we use it?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.

What string method is used to format values in a string?

The Java String. format() method returns the formatted string by a given locale, format, and argument. If the locale is not specified in the String.

Why do we use formatting string?

Format provides give you great flexibility over the output of the string in a way that is easier to read, write and maintain than just using plain old concatenation. Additionally, it's easier to get culture concerns right with String.


2 Answers

Short answer: it cannot be done.


Long answer: the format! macro (and its derivatives) requires a string literal, that is a string known at compilation-time. In exchange for this requirement, if the arguments provided do not match the format, a compilation error is raised.


What you are looking for is known as a template engine. A non-exhaustive list of Rust template engines in no particular order:

  • Handlebars
  • Rustache
  • Maud
  • Horrorshow
  • fomat-macros
  • ...

Template engines have different characteristics, and notably differ by the degree of validation occurring at compile-time or run-time and their flexibility (I seem to recall that Maud was very HTML-centric, for example). It's up to you to find the one most fitting for your use case.

like image 123
Matthieu M. Avatar answered Sep 22 '22 21:09

Matthieu M.


Check out the strfmt library, it is the closest I've found to do dynamic string formatting.

like image 33
opensourcegeek Avatar answered Sep 24 '22 21:09

opensourcegeek