官术网_书友最值得收藏!

How to do it...

In just five steps, we will explore command line I/O and formatting:

  1. Open a Terminal window (PowerShell on Windows) and run the cargo new hello-world command, which creates a new Rust project in a hello-world folder.
  2. Once created, change into the directory with cd hello-world and open src/main.rs with a Visual Studio Code. The default code generated by cargo looks like this:
fn main() {
println!("Hello, world!");
}
  1. Let's expand it! These are variations on the preceding traditional print statement, showing some formatting options, parameters, and writing on streams, among other things. Let's start with some common prints (and imports):
use std::io::{self, Write};
use std::f64;

fn main() {
println!("Let's print some lines:");
println!();
println!("Hello, world!");
println!("{}, {}!", "Hello", "world");
print!("Hello, ");
println!("world!");

However, we can do much more complex argument combinations:

    println!("Arguments can be referred to by their position: {0}, 
{1}! and {1}, {0}! are built from the same arguments", "Hello",
"world");

println!("Furthermore the arguments can be named: \"{greeting},
{object}!\"", greeting = "Hello", object = "World");

println!("Number formatting: Pi is {0:.3} or {0:.0} for short",
f64::consts::PI);

println!("... and there is more: {0:>0width$}={0:>width$}=
{0:#x}", 1535, width = 5);

let _ = write!(&mut io::stdout(), "Underneath, it's all writing
to a stream...");
println!();

println!("Write something!");
let mut input = String::new();
if let Ok(n) = io::stdin().read_line(&mut input) {
println!("You wrote: {} ({} bytes) ", input, n);
}
else {
eprintln!("There was an error :(");
}
}

This should provide several variations of reading and writing to the console.

  1. Go back to Terminal and navigate to the directory where Cargo.toml is located.
  2. Use cargo run to see the snippet's output:
$ cargo run
Compiling hello-world v0.1.0 (/tmp/hello-world)
Finished dev [unoptimized + debuginfo] target(s) in 0.37s
Running 'target/debug/hello-world'
Let's print some lines:

Hello, world!
Hello, world!
Hello, world!
Arguments can be referred to by their position: Hello, world! and world, Hello! are built from the same arguments
Furthermore the arguments can be named: "Hello, World!"
Number formatting: Pi is 3.142 or 3 for short
... and there is more: 01535= 1535=0x5ff
Underneath, it's all writing to a stream...
Write something!
Hello, world!
You wrote: Hello, world!
(14 bytes)

Each line in the output represents a way to print text to the console! We recommend playing with the variations and seeing how it changes the result. On a side note, rustc will check for the correct number of arguments in any println!() or format!() call.

Now, let's go behind the scenes to understand the code better.

主站蜘蛛池模板: 新邵县| 哈巴河县| 五河县| 庆阳市| 九台市| 唐山市| 池州市| 汾阳市| 蒲城县| 辽阳县| 古田县| 延吉市| 崇义县| 仙游县| 盖州市| 深州市| 平乡县| 四子王旗| 拜泉县| 通江县| 长宁县| 宜城市| 忻州市| 和林格尔县| 东莞市| 青浦区| 五大连池市| 永安市| 德格县| 罗源县| 洛南县| 泰顺县| 铜梁县| 门源| 盐源县| 昌吉市| 淄博市| 湘阴县| 错那县| 施秉县| 万州区|