-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add TryFrom<&str> for [char; N]. #93549
Conversation
r? @scottmcm (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
8dab554
to
b0ad48f
Compare
Does this need any new unsafe code? To me it looks like it's essentially this #![feature(array_from_fn)]
pub fn str_to_char_array<const N: usize>(s: &str) -> Option<[char; N]> {
let mut c = s.chars();
let a = std::array::try_from_fn(|_| c.next())?;
if c.next().is_none() {
Some(a)
} else {
None
}
} Or, internally, it could just use rust/library/core/src/array/mod.rs Line 792 in ad88831
But as a meta-point, have you had any conversation with libs folks about this? Because Because of the variable-width encoding in And it's not obvious what the error type should be, to me. This has done a bunch of UTF-8 decoding work where it's not obvious that it should be thrown away -- kinda like how https://doc.rust-lang.org/nightly/std/str/struct.Utf8Error.html#method.valid_up_to exists -- so maybe it wants to be a complicated error type than can return partial results, or something. So overall, this feels way more like it wants to be (Or that it wants a type to fill in the "array is to slice as ???? is to str" problem.) |
Hey @scottmcm thanks for the response! I basically agree with all of what you say. The reason I did it the way I did rather than using I'd like to have a discussion about this with the libs team. Is there a canonical location for chatting (irlo, Zulip)? I'm sorry I'm not super familiar with how rustc dev works (but would like to be more familiar). EDIT I should say more about the impl overlap. It occurs because we want to gather into a |
I would suggest zulip, yes, for feedback from libs-api team members: https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Request.20review.20for.20.2391589/near/270429471 . IRLO is good for broader feedback from more people. As for cc rust-lang/rfcs#2990 which wanted to add |
The motivation question still is open, in any case. It is a potentially quite expensive and complicated conversion to attempt on a sufficiently long string, and one usually better attended to by iteration. What does having the |
@workingjubilee The reason I reached for it is because I was making a wordle clone, and wanted it to work with non-ascii characters. The reason I thought it might belong in libstd was because of its symmetry with |
Note that if you're doing wordle (or programming competition-style stuff), you can just use I'm just always super-skeptical of any constant-length stuff when it comes to strings in general, because of things like how "noël" and "noël" are different lengths. |
Yep I think I've been convinced that this PR is misguided - I'll close it.
Do you have another approach in mind? This is something I'd love to get into std because it's something I would use. |
I would suggest doing it via let av: ArrayVec<char, 10> = "hello".chars().collect(); That way it's still no-allocations-needed, and does a good job of dealing with different lengths. (I personally want something like arrayvec in |
This patch adds the ability to create an array of chars from a
str
similarly to how you can do this with[T] -> [T; N]
(whereT: Copy
). The implementation is slightly more involved than for the slice case because you can't simplymemcpy
the bytes. If you accept this patch, I'd appreciate someone looking over theunsafe
code to check soundness.