4 ms·
In today's Rust you can drop the 'static.
by steveklabnik 9y ago
In today's Rust you can drop the 'static.
- deathanatos 9y agoThis example is one of those things I keep running into though, and don't fully understand: why does type inference not work on consts? e.g., here, why can't we drop the entire type: const USAGE = "usage: blah blah"; The RHS has sufficient type information, does it not? After all, if this were a let USAGE = ... inside a function, that would be sufficient. (I don't find this particular bit a deal-breaker though; thus far, I quite like Rust.)
- steveklabnik 9y agoThis was an explicit decision; we could do this kind of inference but chose not to, similar to how you must declare the types of functions. We started with the most conservative choice, and then are feeling it out; that's why you can now drop the lifetime.
- kibwen 9y agoNot sure why this is being downvoted. The GP commenter is mistaken, the following line is completely valid Rust: const USAGE: &str = "usage: blah blah"; Compare it to the equivalent in C: const char *USAGE = "usage: blah blah";
- CalChris 9y agoAnd while you are at it, compare that to: #define USAGE "usage: blah blah"
- dtolnay 9y agoThe equivalent C needs a second const -- your first one refers to the data behind the string but not the string pointer, and is more like Rust's `let usage: &str`. const char * const USAGE = "usage: blah blah";
- crzwdjk 9y agoOr rather `let mut usage: &str`. Rust actually is kind of similar to C here in that the pointer and pointee can be separately const or mutable. But in Rust "const" is the default and mut needing to be explicitly declared, whereas with C it's the other way around.