Capturing User Input
Hands-on Rust — by Herbert Wolverson (24 / 120)
👈 Creating a New Project | TOC | Moving Input to a Function 👉
Most computer programs operate in a cycle of accepting input from the user and transforming that into some form of — hopefully useful — output. A calculator without buttons is useless, and a computer program without input is equally limited to always doing the same thing. You used println! in “Hello, World” to output text; you can use read_line() to accept data from the terminal.
In this section, you’ll use the terminal to ask the visitor to type their name and receive the result. Finally, you’ll make use of Rust’s formatting system to print a personalized greeting to the terminal.
Prompting for the Visitor’s Name
When a visitor arrives at your swanky new treehouse, you need to ask them for their name. In Printing Text, you used println! to print text to the screen. You’ll do the same thing here, too.
Replace println!(“Hello, World”) with:
FirstStepsWithRust/hello_yourname/src/main.rs
println!("Hello, what's your name?");
Why Did the Project Name Change?
INFORMATION
Don’t worry, you’re still working on the treehouse project. The source code examples in this book are provided in chunks, representing each stage of development…