Skip to content

[Docs] Add richer examples to the Guide to Strings #20854

@velvia

Description

@velvia

As someone coming from dynamic as well as JVM languages, Rust strings were confusing. The Strings guide has very helpful intro material, but soon I found myself stumbling on more sophisticated use cases.

For example, let's say I want a struct where a member stores a list of strings:

pub struct DataRecord {
  pub name: String,
  pub tags: Vec<&'static str>
}

The number of possible choices is bewildering. Do I use [String], Vec<String>, [&str], etc. etc.? Minimizing allocation is good, but structs are tricky because they might be kept around and bounced between multiple function calls before being disposed, or even kept in some other data structure. Also, each string within the list might come from a constant or not. Much more things to think about.

Anyways, the suggestion would be to cover a few examples like this where you use Strings in structs, vecs, arrays etc and go over what are best practices and common scenarios.

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