Skip to content

Add some of [T]’s methods to strings and vice versa#1152

Merged
alexcrichton merged 2 commits into
rust-lang:masterfrom
ftxqxd:string-slice-api-symmetry
Jul 1, 2015
Merged

Add some of [T]’s methods to strings and vice versa#1152
alexcrichton merged 2 commits into
rust-lang:masterfrom
ftxqxd:string-slice-api-symmetry

Conversation

@ftxqxd

@ftxqxd ftxqxd commented Jun 6, 2015

Copy link
Copy Markdown
Contributor

Add some methods that already exist on slices to strings and vice versa. Specifically, the following methods should be added:

  • str::chunks
  • str::windows
  • str::into_string
  • String::into_boxed_slice
  • <[T]>::subslice_offset

Rendered

@alexcrichton alexcrichton added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Jun 6, 2015
@Kimundi

Kimundi commented Jun 6, 2015

Copy link
Copy Markdown
Contributor

Is the reason for adding <[T]>::subslice_offset just symmetry? I'm asking because I'm not sure if that function is actually used by anyone.

@ftxqxd

ftxqxd commented Jun 6, 2015

Copy link
Copy Markdown
Contributor Author

@Kimundi I’ve wanted to use it in the past, but yeah, it’s mainly just to match the str API. I wouldn’t really care if subslice_offset got removed from str (it’s not very useful), but I’d certainly prefer to either have it for both types or neither, rather than just one.

@Gankra

Gankra commented Jun 6, 2015

Copy link
Copy Markdown
Contributor

Is there really a usecase for str::chunks or windows?

@gkoz

gkoz commented Jun 6, 2015

Copy link
Copy Markdown

Out of curiosity, what is Box<str> for? I haven't encountered any mentions of this type before.

@Gankra

Gankra commented Jun 6, 2015

Copy link
Copy Markdown
Contributor

@gkoz In principle it's just a runtime-constructed &'static str (string litteral). one usize thinner than a String, but effectively immutable as a result.

@ftxqxd

ftxqxd commented Jun 6, 2015

Copy link
Copy Markdown
Contributor Author

@gankro str::windows could be used to calculate the Sørensen–Dice coefficient of two strings, and str::chunks has been asked for on IRC a few times.

@huonw

huonw commented Jun 6, 2015

Copy link
Copy Markdown
Contributor

Hm, I suspect windows and chunks will encourage bad Unicode practice without a huge amount of benefit other than symmetry. Specifically they will split between combining characters.

Comment thread text/0000-slice-string-symmetry.md Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“Character” is kinda ambiguous. (Unicode has four different definitions.) Maybe use chars instead?

@krdln

krdln commented Jun 9, 2015

Copy link
Copy Markdown
Contributor

+1 to functions involving Box<str>. I was surprised they weren't implemented. And it's good to lose those 4/8 bytes sometimes (especially when within an enum).

-1 to windows and chunks. I second @huonw that they encourage bad practices. I think they belong in unicode crate with codepoint_ and grapheme_ variants.

@krdln

krdln commented Jun 10, 2015

Copy link
Copy Markdown
Contributor

I just stumbled upon the lack of str::shift_char() counterpart for a slice. I guess it would also be worth adding.

@ftxqxd

ftxqxd commented Jun 10, 2015

Copy link
Copy Markdown
Contributor Author

I just stumbled upon the lack of str::shift_char() counterpart for a slice. I guess it would also be worth adding.

@krdln #1058 already adds that, I believe.

@comex

comex commented Jun 11, 2015

Copy link
Copy Markdown

May be out of scope, but I'd like to see some form of find, rfind, replace, and other pattern-based methods added to [T] or Vec<T>. This would be useful, for example, when dealing with a &[u8] which represents a "string" that may not be valid UTF-8 (such as the contents of a Unix OsStr) - or just random binary data, for that matter.

@Gankra Gankra self-assigned this Jun 11, 2015
@Gankra

Gankra commented Jun 11, 2015

Copy link
Copy Markdown
Contributor

I concur with @huonw on the dubiousness of windows and chunks in the face of combining characters. It's not clear to me how the Sørensen–Dice coefficient is even supposed to interact with utf8 in that regard.

👍 on the Box conversions. Makes sense.

I'm completely indifferent on subslice_offset

@alexcrichton alexcrichton added the final-comment-period Will be merged/postponed/closed in ~10 calendar days unless new substational objections are raised. label Jun 16, 2015
@alexcrichton

Copy link
Copy Markdown
Member

This RFC is now entering its final comment period.

Comment thread text/0000-slice-string-symmetry.md Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would personally prefer to not add this method to slices just yet, I suspect it won't survive stabilization of strings.

@alexcrichton

Copy link
Copy Markdown
Member

I also agree with @huonw about being hesitant to add windows/chunks to strings due to unicode fun stuff.

Comment thread text/0000-slice-string-symmetry.md Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sfackler

Copy link
Copy Markdown
Member

The Box<str> methods seem good to me but I share the unicode weirdness concerns above with respect to chunks and windows and am not sold on the utility of subslice_offset.

@Gankra

Gankra commented Jun 22, 2015

Copy link
Copy Markdown
Contributor

@P1start the libs team seems pretty unanimous about Box<str> is great, everything else is not. Is this an acceptable result of this RFC, for you?

@ftxqxd

ftxqxd commented Jun 22, 2015

Copy link
Copy Markdown
Contributor Author

@gankro Yep, that’s fine by me. I’ll update the RFC soon to include just those methods (and rename into_boxed_slice to into_boxed_str unless anyone has any objections).

@ftxqxd

ftxqxd commented Jun 27, 2015

Copy link
Copy Markdown
Contributor Author

RFC updated, removing str::windows, str::chunks, and <[T]>::subslice_offset.

@alexcrichton

Copy link
Copy Markdown
Member

The consensus of the libs team is to merge this RFC, so I will do so. Thanks again @P1start!

@alexcrichton
alexcrichton merged commit 566bb70 into rust-lang:master Jul 1, 2015
@aturon

aturon commented Jul 1, 2015

Copy link
Copy Markdown
Contributor

Tracking issue.

bluss added a commit to bluss/rust that referenced this pull request Aug 13, 2015
This is the name that was decided in rust-lang/rfcs#1152, and it's
better if we say “boxed str” for `Box<str>`.

The old name `String::into_boxed_slice` is deprecated.
bors added a commit to rust-lang/rust that referenced this pull request Aug 13, 2015
Rename String::into_boxed_slice -> into_boxed_str

This is the name that was decided in rust-lang/rfcs#1152, and it's
better if we say “boxed str” for `Box<str>`.

The old name `String::into_boxed_slice` is deprecated.
bors added a commit to rust-lang/rust that referenced this pull request Aug 14, 2015
Rename String::into_boxed_slice -> into_boxed_str

This is the name that was decided in rust-lang/rfcs#1152, and it's
better if we say “boxed str” for `Box<str>`.

The old name `String::into_boxed_slice` is deprecated.
@Centril Centril added the A-slice Slice related proposals & ideas label Nov 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-slice Slice related proposals & ideas final-comment-period Will be merged/postponed/closed in ~10 calendar days unless new substational objections are raised. T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Projects

None yet

Development

Successfully merging this pull request may close these issues.