Skip to content

Fixes nits in string guide #17453

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

Merged
merged 1 commit into from
Oct 21, 2014
Merged

Fixes nits in string guide #17453

merged 1 commit into from
Oct 21, 2014

Conversation

steveklabnik
Copy link
Member

/cc @huonw

}
```

This is prefereable because it can save you allocating copies of what's inside.
Copy link
Member

Choose a reason for hiding this comment

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

"to be able to call it".

Copy link
Contributor

Choose a reason for hiding this comment

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

s/prefereable/preferable/

Copy link
Member Author

Choose a reason for hiding this comment

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

@EduardoBautista fixed, thanks

@huonw i'm not sure what you mean, can you give me a better diff :)

Copy link
Member

Choose a reason for hiding this comment

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

"this is preferable because it can save you allocating extra storage to be able to call the function." (You're not necessarily allocating copies of the contents, e.g. &[&str] just needs to allocate a Vec<&str> with pointers into the old one).

@steveklabnik
Copy link
Member Author

I threw up http://steveklabnik.github.io/rust-issue-17340/ , but it seems it doesn't actually link to the css, @huonw . Any idea whats up?

@huonw
Copy link
Member

huonw commented Oct 4, 2014

You need to compile like the makefiles do, they insert some headers via rustdoc flags (the easiest way is probably to just compile normally and upload that, but you can also look in mk/docs.mk for the flags).

@huonw
Copy link
Member

huonw commented Oct 4, 2014

(Oh, looks like it's --markdown-css rust.css.)

@steveklabnik
Copy link
Member Author

Done!

```{rust}
fn sum_of_string_length<T: Str>(xs: &[T]) -> uint {
xs.iter().fold(0, |acc, x| acc + x.as_slice().len())
}
Copy link
Member

Choose a reason for hiding this comment

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

This is actually a pattern that we have been discouraging in the past due to the Str trait not quite being what you'd expect, as well as not being quite designed for this purpose. The case of a &[String] vs &[&str] is a little different, but in general if you're using a string and you don't want ownership you should always use &str.

Right now the ergonomics aren't quite where they could be, but there are a few RFCs open about the auto-borrowing issue which should help here.

cc @aturon, do you think something like this should be in the guide for now?

Copy link
Member

Choose a reason for hiding this comment

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

Autoborrowing will never solve &[String] vs. &[&str] since they have different representations.

(Also, this sort of generic code was actually the reason that I added the Str trait.)

Copy link
Member

Choose a reason for hiding this comment

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

I completely agree with @alexcrichton: idiomatic Rust should take &str values, not bound by Str. The ergonomics are going to be improved one way or another before 1.0. The guide should recommend &str.

Copy link
Member

Choose a reason for hiding this comment

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

@huonw is true that &[String] vs &[&str] will not be solved with autoborrowing, but it's still true that this is not what Str is designed for right now.

Copy link
Member

Choose a reason for hiding this comment

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

@aturon note the context here: if you have a &[String], the only way to call a &[&str] function is to allocate a Vec<&str> and place a pile of slices into it.

Copy link
Member

Choose a reason for hiding this comment

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

That said, most instances of this are probably able to take an Iterator<&str>... which that also has flexibility problems, e.g. a Iterator<String> cannot become an Iterator<&str> without collecting to a Vec. On the other hand, collecting to a Vec is always required to call a &[...] function with an Iterator so taking an Iterator<&str> is still a strict improvement.

Copy link
Member

Choose a reason for hiding this comment

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

@huonw, ah I see; I had missed that.

Even so, bounding by Str seems a bit unfortunate somehow -- I think because it seems a bit ad hoc/overly specific.

I actually wonder if Borrow is the more general concept lurking here? I.e., maybe you want to take &[T] where T: Borrow<str>? Then the same solution would work for vectors (and eventually other sliceable types).

Copy link
Member

Choose a reason for hiding this comment

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

@alexcrichton &str doesn't need an as_slice method, except for use in generic code. :)

Copy link
Member

Choose a reason for hiding this comment

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

@aturon yeah, maybe Borrow/Deref are what we want.

(However, I do think that this is rare enough that we can avoid mentioning it in the guides until we've got our story straight.)

Copy link
Member

Choose a reason for hiding this comment

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

@huonw re: Borrow/Deref distinction, see rust-lang/rfcs#235 (comment)

@aturon
Copy link
Member

aturon commented Oct 17, 2014

What's the status on on this?

@steveklabnik
Copy link
Member Author

I posted the in-progress version, waiting on @huonw 's feedback.

@huonw
Copy link
Member

huonw commented Oct 17, 2014

I'm fine with it as it is, although I'm under the impression that @aturon and @alexcrichton would prefer that the generic Str section just be removed for now until we have our story in that area sorted out.

@steveklabnik
Copy link
Member Author

Is that the conclusion here, @aturon and @alexcrichton ? I can do that if that's the path we want to go down.

@alexcrichton
Copy link
Member

Yeah let's remove the generic Str for now.

@steveklabnik
Copy link
Member Author

Cool, i'll do that as soon as this make check is done running :)

@steveklabnik
Copy link
Member Author

Removed, rebased, and squashed.

@bors bors closed this Oct 21, 2014
@bors bors merged commit f358407 into rust-lang:master Oct 21, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants