How would you stream output from a Process?

Although the accepted answer is correct, it doesn’t cover the non-trivial case. To stream output and handle it manually, use Stdio::piped() and manually handle the .stdout property on the child returned from calling spawn, like this: use std::process::{Command, Stdio}; use std::path::Path; use std::io::{BufReader, BufRead}; pub fn exec_stream<P: AsRef<Path>>(binary: P, args: Vec<&’static str>) { let mut … Read more

How can I unpack (destructure) elements from a vector?

It seems what you need is “slice patterns”: fn main() { let line = “127.0.0.1 1000 what!?”; let v = line.split_whitespace().take(3).collect::<Vec<&str>>(); if let [ip, port, msg] = &v[..] { println!(“{}:{} says ‘{}'”, ip, port, msg); } } Playground link Note the if let instead of plain let. Slice patterns are refutable, so we need to … Read more

How to read a specific number of bytes from a stream?

Since Rust 1.6, Read::read_exact can be used to do this. If bytes_to_read is the number of bytes you need to read, possibly determined at runtime, and reader is the stream to read from: let mut buf = vec![0u8; bytes_to_read]; reader.read_exact(&mut buf)?; The part that wasn’t clear to me from the read_exact documentation was that the … Read more

Slice a string containing Unicode chars

Possible solutions to codepoint slicing I know I can use the chars() iterator and manually walk through the desired substring, but is there a more concise way? If you know the exact byte indices, you can slice a string: let text = “Hello привет”; println!(“{}”, &text[2..10]); This prints “llo пр”. So the problem is to … Read more

Linking Rust application with a dynamic library not in the runtime linker search path

Here’s a Minimal, Reproducible Example that exhibits the same problem that you experienced. I created a C library exporting a simple addition function. I also created a Cargo project to use this function. dynlink/ ├── executable │   ├── build.rs │   ├── Cargo.lock │   ├── Cargo.toml │   └── src │      └── main.rs └── library ├── … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)