Read large files line by line in Rust [duplicate]

You want to use the buffered reader, BufRead, and specifically the function BufReader.lines(): use std::fs::File; use std::io::{self, prelude::*, BufReader}; fn main() -> io::Result<()> { let file = File::open(“foo.txt”)?; let reader = BufReader::new(file); for line in reader.lines() { println!(“{}”, line?); } Ok(()) } Note that you are not returned the linefeed, as said in the documentation. … Read more

How to implement a custom ‘fmt::Debug’ trait?

According to the example from the std::fmt docs: extern crate uuid; use uuid::Uuid; use std::fmt; struct BlahLF { id: Uuid, } impl fmt::Debug for BlahLF { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, “Hi: {}”, self.id) } } The part to emphasize is the fmt:: in fmt::Result. Without that you’re referring to the … Read more

How to generate statically linked executables?

Since Rust 1.19, you can statically link the C runtime (CRT) to avoid this very common situation on Windows: The program can’t start because VCRUNTIME140.dll is missing from your computer. Try reinstalling the program to fix this problem. Add this to your .cargo/config file, using the appropriate target triple for your platform: [target.x86_64-pc-windows-msvc] rustflags = … Read more

Understanding Traits and Object Safety

Why does this particular example not work? The chapter Trait Objects states: So what makes a method object-safe? Each method must require that Self: Sized Isn’t that fulfilled? This question really is: What is a trait object? A trait object is an interface in the Object-Oriented paradigm: it exposes a limited set of methods, which … Read more

How to store rusqlite Connection and Statement objects in the same struct in Rust? [duplicate]

Let’s look at Connection::prepare: pub fn prepare<‘a>(&’a self, sql: &str) -> Result<Statement<‘a>> If we ignore the Result (which just means that this function can fail), this means “return a Statement that can live no longer than the Connection that prepare was called on”. This is likely due to the Statement containing a reference to the … Read more

Why would I use divergent functions?

It has several uses. It can be used for functions which are designed to panic or exit the program. panic!() itself is one such function, but it can also be applied to functions which wrap panic!(), such as printing out more detailed error information and then panicking. It can also be used for functions that … Read more