Why does the Rust compiler not optimize code assuming that two mutable references cannot alias?

As far as I know, reference/pointer aliasing can hinder the compiler’s ability to generate optimized code, since they must ensure the generated binary behaves correctly in the case where the two references/pointers indeed alias. For instance, in the following C code, void adds(int *a, int *b) { *a += *b; *a += *b; } when … Read more

How to disable unused code warnings in Rust?

struct SemanticDirection; fn main() {} warning: struct is never used: `SemanticDirection` –> src/main.rs:1:1 | 1 | struct SemanticDirection; | ^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: #[warn(dead_code)] on by default I will turn these warnings back on for anything serious, but I am just tinkering with the language and this is driving me bats. I tried adding #[allow(dead_code)] … Read more

Why doesn’t println! work in Rust unit tests?

I’ve implemented the following method and unit test: use std::fs::File; use std::path::Path; use std::io::prelude::*; fn read_file(path: &Path) { let mut file = File::open(path).unwrap(); let mut contents = String::new(); file.read_to_string(&mut contents).unwrap(); println!(“{}”, contents); } #[test] fn test_read_file() { let path = &Path::new(“/etc/hosts”); println!(“{:?}”, path); read_file(path); } I run the unit test this way: rustc –test app.rs; … Read more