Rust Notes

· archit's blog


Building a simple shell #

 1use std::io::{self, Write};
 2use std::process::{Command, Stdio};
 3
 4fn main() {
 5    loop {
 6        // Use the `>` character as the prompt
 7        // Need to explicitly flush this to ensure it prints before read_line
 8        print!("Shell> ");
 9        io::stdout().flush().unwrap();
10
11        let mut input = String::new();
12        io::stdin().read_line(&mut input).expect("Failed to read line");
13
14        let command = input.trim();
15
16        let mut child = Command::new(command)
17            .stdin(Stdio::inherit())
18            .stdout(Stdio::inherit())
19            .stderr(Stdio::inherit())
20            .spawn()
21            .expect("Failed to execute command");
22
23        // Wait for the child process to complete
24        child.wait().expect("Failed to wait for child process");
25    }
26}

**Part by Part explanation:

1        print!("Shell> ");
2        io::stdout().flush().unwrap();
1        let mut input = String::new();
2        io::stdin().read_line(&mut input).expect("Failed to read line");
1        let mut child = Command::new(command)
2            .stdin(Stdio::inherit())
3            .stdout(Stdio::inherit())
4            .stderr(Stdio::inherit())
5            .spawn()
6            .expect("Failed to execute command");
1       child.wait().expect("Failed to wait for child process");

Overall, this Rust program acts as a simple shell that repeatedly accepts user commands, executes them as separate processes, and waits for the processes to complete before accepting the next command. It provides a basic interactive shell-like experience.

Building a Protocol #

Building a deep learning inference system #

ds

Integrating Stuff with #wgpu & #wasm #