example_echo_hello_world/
example-echo-hello-world.rs

1#![doc(html_playground_url = "https://play.rust-lang.org/")]
2//! # Get the ouput from `echo Hello, world!`
3//!
4//! ```
5//! use std::process::Command;
6//! let output = Command::new("echo")
7//!        .arg("Hello,")
8//!        .arg("world!")
9//!        .output()
10//!        .expect("Failed to execute command");
11//! let output_1 = String::from_utf8_lossy(&output.stdout).to_string();
12//! assert_eq!(format!("{}", output_1), "Hello, world!\n");
13//! let output_2 = String::from_utf8(output.stdout).expect("Format error");
14//! assert_eq!(format!("{}", output_2), "Hello, world!\n");
15//!
16//! ```
17//! [std::process::Command]
18//!
19//! [String::from_utf8]
20//!
21//! [String::from_utf8_lossy]
22
23use std::process::Command;
24
25fn echo_hello_world() -> String {
26    let output = Command::new("echo")
27        .arg("Hello,")
28        .arg("world!")
29        .output()
30        .expect("Failed to execute command");
31    String::from_utf8(output.stdout).expect("Format error")
32}
33
34fn main() {
35    println!("{}", echo_hello_world());
36}
37
38#[cfg(test)]
39#[test]
40fn test_echo_hello_world() {
41    assert_eq!(format!("{}", echo_hello_world()), "Hello, world!\n");
42}