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

Managing the lifetimes of garbage-collected objects [closed]

Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 months ago. The community reviewed whether to reopen this question last month and left it closed: Original close reason(s) were … Read more

How to match a String against string literals?

I’m trying to figure out how to match a String in Rust. I initially tried matching like this, but I figured out Rust cannot implicitly cast from std::string::String to &str. fn main() { let stringthing = String::from(“c”); match stringthing { “a” => println!(“0”), “b” => println!(“1”), “c” => println!(“2”), } } This has the error: … 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