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

Implement Mul<usize> and MulAssign<usize> for String #41350

Closed
wants to merge 2 commits into from
Closed

Implement Mul<usize> and MulAssign<usize> for String #41350

wants to merge 2 commits into from

Conversation

imp
Copy link

@imp imp commented Apr 17, 2017

This PR adds ability to multiply the content of the String. Those with Python experience may find the implemented behaviour familiar.

let one = String::from("one");
assert_eq!(one * 3, "oneoneone");

let mut one = String::from("one");
one *= 3;
assert_eq!(one, "oneoneone");

I followed the precedence set by #34890 when implementing Mul and MulAssign traits.

Also this is my very first contribution to the Rust itself, so if I missed anything important I'd be glad to learn.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @BurntSushi (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@BurntSushi
Copy link
Member

I'm pretty sure this has been discussed in the past and we decided against additional impls like this on strings. I don't know where that discussion is though.

cc @rust-lang/libs

@imp
Copy link
Author

imp commented Apr 17, 2017

Well, in that case it might be useful to list the reasoning here. At least for the sake of posterity.

@frewsxcv frewsxcv added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Apr 18, 2017
@leonardo-m
Copy link

leonardo-m commented Apr 18, 2017

A disadvantage: it hides a memory allocation (under syntax that's hard to grep). This is fine in Python, but in a tidy system language you want to avoid that.

An advantage: if you have programmed in Python you know that this operation is sometimes useful. But is it sufficiently useful for Rust? This pull request needs to list some use cases for Rust code.

A typical use case for Python it to make ASCII tables:

x = 2.56
y = "hello"
sx = "%f %s" % (x, y)
print "-" * len(sx)
print sx
print "-" * len(sx)

Gives:

--------------
2.560000 hello
--------------

@KalitaAlexey
Copy link
Contributor

@imp
Copy link
Author

imp commented Apr 18, 2017

@KalitaAlexey Right. That seems to fit the bill. Apparently it is relatively new and was not available when I first encountered lack of multiplication. Thanks for pointing it out.
With that in mind I am pulling back this PR.

@imp imp closed this Apr 18, 2017
@imp imp deleted the stringmultiply branch April 20, 2017 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants