Can someone explain the special syntax behind String constants in Rust?
```
static COLOR: &'static str = "red";
```
Why is it so different from numeric constants?
```
static SIZE : usize = 999;
```
@rustlang
-
-
Replying to @_ddrscott_ @rustlang
It's not that special under the hood (but more special than it probably should be). & means it's a reference to memory; an str is an unsized UTF-8 string. The reference is "fat" which means it has a length (this is implied by str being unsized). Static is the program lifetime.
1 reply 0 retweets 2 likes -
So there's a lot of information packed into a string. By contrast, a usize is just a usize--it has no extra size, isn't a reference, and as a result doesn't need an explicit lifetime (since you can just copy it to wherever you need it).
1 reply 0 retweets 3 likes -
-
Replying to @_ddrscott_ @rustlang
That's what delineates a lifetime in Rust. There are some who think they should've figured out a way to get rid of it, but it's been set in stone for a while now.
1 reply 0 retweets 1 like
Yeah, we inherited the syntax from ocaml, though it uses it for something else. We wanted something better but after years nobody could come up with anything, so it stuck
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.