How to generate right output from this code?

This is example code in The Book about variables.

"Say our program asks a user to show how many spaces they want between some text by inputting space characters, and then we want to store that input as a number."

    let spaces = "   ";
    let spaces = spaces.len();

i was run and add print code:

    let spaces = "   ";
    let spaces = spaces.len();
    println! ("{spaces}");

The code is run smoothly with output text spaces, not a number. Is this program need more code like guessing game to get how many spaces between a text?.

Thank you.

Are you using an old version of Rust? There isn't anything wrong with your code, but the formatting syntax that can use variables like that was added early last year in 1.58.0. You can check your Rust version with rustc --version.

If you are, and you've installed Rust with rustup, you can update it with rustup update. If you've installed it some other way and don't want to switch to rustup/can't update to a newer version, you can read an older version of the book, for example https://doc.rust-lang.org/1.57.0/book/ch03-01-variables-and-mutability.html (you can replace the 1.57.0 with whatever version you're using).

1 Like

aha, my rust version is 1.26.0. Thank you, ongoing to .78.

1 Like

That's from over 5 years ago -- May 2018 -- so definitely a good idea to update :laughing:

4 Likes

Fact that surprised me :rofl:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.