Rust System information gathering program
Rust Basic: Exercise-1 with Solution
Write a Rust program that gathers system information such as the Rust version, operating system details, and system architecture.
Sample Solution:
Rust Code:
use std::process::Command;
fn main() {
// Get Rust version
let rust_version_output = Command::new("rustc").arg("--version").output().expect("Failed to execute rustc");
let rust_version = String::from_utf8_lossy(&rust_version_output.stdout);
println!("Rust version: {}", rust_version);
// Get system information
let uname_output = Command::new("uname").arg("-a").output().expect("Failed to execute uname");
let system_info = String::from_utf8_lossy(&uname_output.stdout);
println!("System information: {}", system_info);
// Get system architecture
let arch_output = Command::new("uname").arg("-m").output().expect("Failed to execute uname");
let system_arch = String::from_utf8_lossy(&arch_output.stdout);
println!("System architecture: {}", system_arch);
}
Output:
Rust version: rustc 1.75.0 (82e1608df 2023-12-21) System information: Linux 38ef7cb6fa68 5.15.0-1052-aws #57~20.04.1-Ubuntu SMP Mon Jan 15 17:04:56 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux System architecture: x86_64
Explanation:
Here's a brief explanation of the above Rust code:
- use std::process::Command;: This line imports the Command struct from the std::process module, which allows us to run external commands.
- fn main() { ... }: This is the entry point of the program, where execution begins.
- let rust_version_output = Command::new("rustc").arg("--version").output().expect("Failed to execute rustc");: This line creates a new Command to execute the rustc --version command. It then uses the output() method to run the command and capture its output. The expect() method is used to handle any errors that may occur during command execution.
- let rust_version = String::from_utf8_lossy(&rust_version_output.stdout);: This line converts the output of the rustc --version command (which is a byte array) into a 'String'. The from_utf8_lossy() function is used to handle any invalid UTF-8 sequences in the output.
- println!("Rust version: {}", rust_version);: This line prints the Rust version obtained from the rustc --version command.
- let uname_output = Command::new("uname").arg("-a").output().expect("Failed to execute uname");: This line creates a new Command to execute the uname -a command to get system information.
- let system_info = String::from_utf8_lossy(&uname_output.stdout);: Similar to before, this line converts the output of the uname -a command into a 'String'.
- println!("System information: {}", system_info);: This line prints the system information obtained from the uname -a command.
- let arch_output = Command::new("uname").arg("-m").output().expect("Failed to execute uname");: This line creates another Command to execute the uname -m command to get the system architecture.
- let system_arch = String::from_utf8_lossy(&arch_output.stdout);: Again, this line converts the output of the uname -m command into a String.
- println!("System architecture: {}", system_arch);: Finally, this line prints the system architecture obtained from the uname -m command.
Rust Code Editor:
Previous: Rust Basic Exercises.
Next: Rust Program: Adding two numbers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/rust/basic/rust-basic-exercise-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics