Skip to content

Add example showing how one can use owned options#594

Merged
mgeisler merged 1 commit intomasterfrom
owned-options
Sep 28, 2025
Merged

Add example showing how one can use owned options#594
mgeisler merged 1 commit intomasterfrom
owned-options

Conversation

@mgeisler
Copy link
Owner

The textwrap::Options<'a> type is a little annoying: it insists on borrowing all its fields from somewhere else. In particular, the indentation fields are of type &'a str. This means that it’s not really possible for a program to store an Options<'a> and use it later if the fields are anything but string literals (of type &str).

However, what does work is to create the Options<'a> on demand. Since all functions that need the options take an argument of type impl Options<'a>, you can easily convert your own type as needed. This can be as cheap as possible, i.e., converting String fields to &str.

From the discussion in #592.

@mgeisler mgeisler enabled auto-merge September 28, 2025 19:21
@mgeisler mgeisler disabled auto-merge September 28, 2025 19:21
The `textwrap::Options<'a>` type is a little annoying: it insists on
borrowing all its fields from somewhere else. In particular, the
indentation fields are of type `&'a str`. This means that it’s not
really possible for a program to store an `Options<'a>` and use it
later if the fields are anything but string literals (of type `&str`).

However, what does work is to create the `Options<'a>` on demand.
Since all functions that need the options take an argument of type
`impl Options<'a>`, you can easily convert your own type as needed.
This can be as cheap as possible, i.e., converting `String` fields to
`&str`.

From the discussion in #592.
@mgeisler mgeisler enabled auto-merge September 28, 2025 19:34
@mgeisler mgeisler merged commit e56a6c6 into master Sep 28, 2025
26 checks passed
@mgeisler mgeisler deleted the owned-options branch September 28, 2025 19:35
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.

1 participant