-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Statically sized string and byte string literals #339
Conversation
I encountered this issue while learning Rust. To summarise: I'm working through the Matasano Crypto Challenges as a way to learn Rust, and as you might expect I've wound up with a bunch of functions that use 16-byte blocks for various things (keys, initialisation vectors, encrypted output, etc.). I'd like to encode these 16-byte blocks into Rust's type-system as I am less concerned about fixed-length string-literals. Since the syntax for a string slice doesn't resemble an array, I didn't expect it to support array-ish things like fixed-length (besides, would it be byte-length or codepoint-length, etc. etc.) |
All these conventions have already settled for runtime strings, we can just bring them to compile time without changes. |
@petrochenkov thanks for writing up this RFC! I think at this time a fixed-length string syntax may be a little too ambitious, but I'd love to see some clarifications for the byte literal syntax! Moving forward with this RFC, would you be ok extracting out the fixed-length strings for now, and considering them a candidate for a future RFC? With respect to byte literals, however, I'd love to help you move this RFC forward. You take an interesting approach in this RFC by having all literals produce the borrowed version of their contents as opposed to the owned version. This has interesting implications with Instead of going the route of borrowing, what would you think of a rule like:
This would mean that all byte literals would have the type I suppose as a knee-jerk reaction I would expect something like:
Those are all a little different which seems odd, though, and it would certainly be nice to reconcile them. I don't think we can get away with Hm, just some thoughts of mine, what do you think? |
I actually think it would be better for fixed-length strings and string slices to live in the library and not in the core language (see "Unresolved questions"), so the
That's unfortunate. At least it doesn't apply to fixed-length (byte) strings, since they are always
What is need to be done? |
Currently there's no way to parameterize over the In the past special case for things like
I suppose the first question to answer is what all these literals should produce. Today So, along those lines, here's some questions we should answer:
I would currently probably answer "fixed size array" to the first question, and "reference" to the second one. Perhaps in the future So, in terms of moving forward: what do you think of the two questions, and what would you answer them with? The RFC will need to be updated to removing the fixed-length string syntax (but certainly mention it as a future extension!) and be updated with whatever decision we make. Also, remember that you don't personally have to implement this! |
Not
I just want to lay the ground and ensure the future compatibility, fixed-length strings don't need to be useful right now. Fixed-length arrays are also much less useful today than they could be.
Okay, let array literals stay as they are. (Although, I'm still interested in the concrete reasons.)
I agree.
I again do not understand why to wait.
My answers to the two questions are the same as yours - b"abcd" should have type
I know, I was just trying to study the compiler and it seemed like a relatively easy exercise to start. |
I'm personally unaware of any plans, nor what's the motivation for doing so.
In theory this could be added backwards-compatibly, right?
While that syntax is certainly plausible, there are many possible routes to go in about the precise syntax here. I suspect that the question for I personally would not expect |
Agree
The key requirement here is the autocoercion from reference to fixed string to string slice an we are unable to meet it now without exposing Okay, I agree, it deserves a separate discussion and a better solution will be possible after gaining the ability to parameterize on integers.
I will change the "do it now" wording to something like "ensure it will be possible in the future" |
Yeah that seems more aligned with what I might expect, |
Updated. |
Looks good to me, thanks @petrochenkov! I'll see if I can bring this up at this week's meeting. |
Related discussion: http://www.reddit.com/r/rust/comments/2ifkyi/why_is_byte_literal_a_static_u8/ |
Let's just go to a plain old `Mutex` instead of trying to be fancy with an opportunistic lock. It weeds out more bugs and we can deal with perf improvements later if a correct implementation comes along. Closes rust-lang#339
Rendered.
Previous discussion.