Why is there a large performance impact when looping over an array with 240 or more elements?

Summary: below 240, LLVM fully unrolls the inner loop and that lets it notice it can optimize away the repeat loop, breaking your benchmark. You found a magic threshold above which LLVM stops performing certain optimizations. The threshold is 8 bytes * 240 = 1920 bytes (your array is an array of usizes, therefore the … Read more

Why are explicit lifetimes needed in Rust?

The other answers all have salient points (fjh’s concrete example where an explicit lifetime is needed), but are missing one key thing: why are explicit lifetimes needed when the compiler will tell you you’ve got them wrong? This is actually the same question as “why are explicit types needed when the compiler can infer them”. … Read more

How do I create a global, mutable singleton?

Non-answer answer Avoid global state in general. Instead, construct the object somewhere early (perhaps in main), then pass mutable references to that object into the places that need it. This will usually make your code easier to reason about and doesn’t require as much bending over backwards. Look hard at yourself in the mirror before … Read more

How do I split a string in Rust?

Use split() let mut split = “some string 123 ffd”.split(“123”); This gives an iterator, which you can loop over, or collect() into a vector. for s in split { println!(“{}”, s) } let vec = split.collect::<Vec<&str>>(); // OR let vec: Vec<&str> = split.collect();

Why are Rust executables so huge?

Rust uses static linking to compile its programs, meaning that all libraries required by even the simplest Hello world! program will be compiled into your executable. This also includes the Rust runtime. To force Rust to dynamically link programs, use the command-line arguments -C prefer-dynamic; this will result in a much smaller file size but … Read more

Package with both a library and a binary?

Tok:tmp doug$ du -a 8 ./Cargo.toml 8 ./src/bin.rs 8 ./src/lib.rs 16 ./src Cargo.toml: [package] name = “mything” version = “0.0.1” authors = [“me <me@gmail.com>”] [lib] name = “mylib” path = “src/lib.rs” [[bin]] name = “mybin” path = “src/bin.rs” src/lib.rs: pub fn test() { println!(“Test”); } src/bin.rs: extern crate mylib; // not needed since Rust edition … Read more

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