What are Rust’s exact auto-dereferencing rules?

I’m learning/experimenting with Rust, and in all the elegance that I find in this language, there is one peculiarity that baffles me and seems totally out of place. Rust automatically dereferences pointers when making method calls. I made some tests to determine the exact behaviour: struct X { val: i32 } impl std::ops::Deref for X … Read more

Why can’t I store a value and a reference to that value in the same struct?

I have a value and I want to store that value and a reference to something inside that value in my own type: struct Thing { count: u32, } struct Combined<‘a>(Thing, &’a u32); fn make_combined<‘a>() -> Combined<‘a> { let thing = Thing { count: 42 }; Combined(thing, &thing.count) } Sometimes, I have a value and … Read more

Convert a String to int?

Note: this question contains deprecated pre-1.0 code! The answer is correct, though. To convert a str to an int in Rust, I can do this: let my_int = from_str::<int>(my_str); The only way I know how to convert a String to an int is to get a slice of it and then use from_str on it … Read more