timClicks (Tim McNamara)
News
Integer division is confusing
Can you guess what the output of this program is? // Program A fn main() { let a = 100; let b = 1000; println!("{}", a (b/a)); println!("{}", a (a/b)); } What about this one? // Program B fn main() { let a = 1000; let b = 100; println!("{}", a (b/a)); pri
Read more
Report
How to use compression algorithms in Rust
Work in Progress Your feedback is welcome. Contact me via the social media links at the top of the page. Creating a uniform interface Each library gets to decide how it works. That makes things relatively complicated when you try to compare one against an
Read more
Report
How to accept a file as a command-line argument in Rust
Using String as a starting point It's common to ask users where to save files. When defining command-line arguments with clap, it's easy to request these as a String. use clap::Parser; #derive(Debug, Parser) struct Args { #clap(long) save_to: String, } Us
Read more
Report
Compile-time assertions
An example Here's a snippet from the smol_str crate that demonstrates a neat trick: const _: () = { assert!(WS.len() == N_NEWLINES + N_SPACES); assert!(WS.as_bytes()N_NEWLINES - 1 == b'n'); assert!(WS.as_bytes()N_NEWLINES == b' '); }; These 5 lines of cod
Read more
Report
Parse CLI arguments without 3rd party crates
Sometimes, bringing in a third-party dependency, such as clap, to your project can feel like a bit of a burden. For example, to improve your prospective users' first impressions, you might want your examples to compile as quickly as possible.It's tempting
Read more
Report
How to create a 2D matrix in Rust
This article shows you two methods for dynamically creating a two-dimensional matrix with the Rust programming language. If we want to fill up our vector of vectors with a simple constant, such as 0, then there's not much work to do. We can use a nested c
Read more
Report
Convert a UNIX timestamp to Rust
Sometimes you'll find yourself with a UNIX timestamp in a CSV file or some other external data source. That integer's a fairly straight-forward way to store the time, but it's not very useful to a Rust programmer. Working Code Example For people who light
Read more
Report
Explaining Rust's Deref trait
On the surface, Deref is merely the trait that is invoked when the "dereference operator", the sometimes unwelcome and intimidating a unary prefixed asterisk (T), is invoked. That is, is describes to Rust how to take a reference as an argument and return
Read more
Report
