3

When I try to initialize a String with the placeholder {} using the following:

let range_from: u32 = 1;
let range_to: u32 = 101;
let insert_message = String::new("Please input Your guess in the range from {} to {}.", range_from, range_to);
println!("{}", insert_message);
// snip
println!("{}", insert_message);

It throws the following Error:

supplied 3 arguments | | | expected 1 argument

The Fabio
  • 5,369
  • 1
  • 25
  • 55
Jonas
  • 513
  • 1
  • 5
  • 17
  • 3
    The macro [`format!`](https://doc.rust-lang.org/std/macro.format.html) creates a String, you could use that (`format!("... {} ... {}", range_from, range_to)`). – Sebastián Palma Jan 23 '22 at 11:53
  • 1
    Awesome @SebastianPalma, Your suggestion works. If You turn it into an answer i am happy to mark it as the solution :) – Jonas Jan 23 '22 at 11:58

1 Answers1

5

String::new can't do that. You can use the format! macro, like this:

let insert_message = format!("Please input Your guess in the range from {} to {}.", range_from, range_to);

Or, as of Rust 1.58, you can also do it like this:

let insert_message = format!("Please input Your guess in the range from {range_from} to {range_to}.");

See this for more.

at54321
  • 8,726
  • 26
  • 46