Skip to content
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

New str and String API #1253

Closed
bluss opened this issue Aug 13, 2015 · 10 comments
Closed

New str and String API #1253

bluss opened this issue Aug 13, 2015 · 10 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@bluss
Copy link
Member

bluss commented Aug 13, 2015

Proposed new str or String API

  • String::insert_str(&mut self, index: usize, s: &str)
    Just like insert(&mut self, usize, char), but for a string piece. Insertion is inherently inefficient, but this impl will be better than the user having to create an entirely new String.
  • String::splice<R>(&mut self, range: R, s: &str)
    Combine drain and insert_str, allow both removing a range and replacing it with a new string in one operation. Remove range, insert s at range.start.
  • str::repeat(&self, n: usize) -> String
    Convenience to repeat a String. Will size the result correctly up front, so it's both simpler and faster than in-code iterator based solutions.
@Stebalien
Copy link
Contributor

splice would be a good method to have on Vec as well.

@bluss
Copy link
Member Author

bluss commented Aug 13, 2015

Yep. I've started experimental implementations here for the moment, including Vec::splice. It doesn't compile perfectly though since it uses iterator input. But it's a nice real use of ExactSizeIterator.

@Gankra
Copy link
Contributor

Gankra commented Aug 13, 2015

fwiw splice could be insert with Sufficient Generics.

@eefriedman
Copy link
Contributor

Relevant motivating example: https://github.com/servo/rust-url/blob/2be081e5fad04efc321e51f795aa2b95d21f4c10/src/parser.rs#L563 uses unsafe code to emulate a special case of splice.

@Kimundi
Copy link
Member

Kimundi commented Aug 14, 2015

I'd like it if rather than adding new _str() variants, the existing ones taking char would just be made generic.

@bluss
Copy link
Member Author

bluss commented Aug 14, 2015

Ok. .push(char) and .push_str(&str) is the current precedent, but I agree.

@blaenk
Copy link
Contributor

blaenk commented Aug 24, 2015

This sounds good to me bluss.

@cristicbz
Copy link

Isn't x.insert(index, y) equivalent to x.splice(index..index, y)? Do we need both?

@dbrodie
Copy link

dbrodie commented Dec 7, 2015

Is there a reason not to have splice return an iterator over the removed values? That seems like a useful addition to have.

The implementation would just need to replace the items one at a time, as each one is taken from the returned iterator, and fallback to drain-like behavior otherwise.

@nrc nrc added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Aug 25, 2016
@mbrubeck
Copy link
Contributor

rust-lang/rust#34771 added insert_str.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

10 participants