Skip to content

String guide nits #17340

Closed
Closed
@huonw

Description

@huonw
  • The codepoint iterator chars is not mentioned in http://doc.rust-lang.org/master/guide-strings.html#indexing-strings

  • The use of Str in http://doc.rust-lang.org/master/guide-strings.html#generic-functions is not particularly idiomatic; since T is at the 'top level' taking a &str directly is fine; being generic over Str is better when it may be expensive for the user to get to a &str, e.g. if the string data is inside something

    fn foo(x: &[&str])
    
    fn bar<T: Str>(x: &[T])
    

    If the user has &[String] and wants to call foo, they're forced to allocate storage to store the .as_slice's of each element, which can be arbitrarily expensive (they may have a lot of Strings); bar gets around this since it can be called with &[String] or &[&str].

    (FWIW, this is actually still true for functions taking iterators, even though one can sometimes .map(|s| s.as_slice()) to get a Iterator<&str>. If you have a Iterator<String>, there's no way to convert that into a Iterator<&str> without collecting into a temporary data structure.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions