What is this question mark operator about?
I’m reading the documentation for File: //.. let mut file = File::create(“foo.txt”)?; //.. What is the ? in this line? I do not recall seeing it in the Rust Book before. 3 Answers 3
I’m reading the documentation for File: //.. let mut file = File::create(“foo.txt”)?; //.. What is the ? in this line? I do not recall seeing it in the Rust Book before. 3 Answers 3
How do you access a Cargo package’s metadata (e.g. version) from the Rust code in the package? In my case, I am building a command line tool that I’d like to have a standard –version flag, and I’d like the implementation to read the version of the package from Cargo.toml so I don’t have to … Read more
By following this guide I created a Cargo project. src/main.rs fn main() { hello::print_hello(); } mod hello { pub fn print_hello() { println!(“Hello, world!”); } } which I run using cargo build && cargo run and it compiles without errors. Now I’m trying to split the main module in two but cannot figure out how … Read more
What is the best way to create and use a struct with only one instantiation in the system? Yes, this is necessary, it is the OpenGL subsystem, and making multiple copies of this and passing it around everywhere would add confusion, rather than relieve it. The singleton needs to be as efficient as possible. It … Read more
Just having found Rust and having read the first two chapters of the documentation, I find the approach and the way they defined the language particularly interesting. So I decided to get my fingers wet and started out with Hello world… I did so on Windows 7 x64, btw. fn main() { println!(“Hello, world!”); } … Read more
From the documentation, it’s not clear. In Java you could use the split method like so: “some string 123 ffd”.split(“123”); 6 Answers 6
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
I would like to make a Rust package that contains both a reusable library (where most of the program is implemented), and also an executable that uses it. Assuming I have not confused any semantics in the Rust module system, what should my Cargo.toml file look like? 4 Answers 4
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
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