Creating a new file based on template

I want to create a file named default.project.json with it's contents to be the default variable. The issue I am having is that I need to replace the name key in the json with &args.name. It won't let me format it because it isn't a string literal.

format! is a macro that runs at compile time instead of at runtime, so it requires the first argument to be a string literal. Something like let default = format!("your{}string{}here", &args.name); would get you moving.
If you need the template to be dynamic I'm not sure there is a way to do it with format!, but there are a bunch of other templating crates out there e.g. GitHub - djc/askama: Type-safe, compiled Jinja-like templates for Rust

rust - println! error: expected a literal / format argument must be a string literal - Stack Overflow has some more interesting information.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.