rust-lang / rust-lang/rust

`str::Lines<'a>` is invariant over `'a` instead of covariant

Open
#160,887 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-iterators A-lifetimes A-str A-variance C-bug S-has-mcve T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I would expect code like this to compile:

fn covariant<'a, 'b: 'a>(x: str::Lines<'b>) -> str::Lines<'a> { x }

Instead, it fails with this error:

error: lifetime may not live long enough
 --> src/lib.rs:1:65
  |
1 | fn covariant<'a, 'b: 'a>(x: str::Lines<'b>) -> str::Lines<'a> { x }
  |              --  -- lifetime `'b` defined here                  ^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a`
  |              |
  |              lifetime `'a` defined here
  |
  = help: consider adding the following bound: `'a: 'b`
  = note: requirement occurs because of the type `std::str::Lines<'_>`, which makes the generic argument `'_` invariant
  = note: the struct `std::str::Lines<'a>` is invariant over the parameter `'a`
  = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance

Because str::Lines<'a> is invariant over 'a instead of covariant over 'a.

Covariance would be particular useful in combination with a crate like yoke, which would allow yoke::Yokeable to be implemented on str::Lines<'static>, allowing yoke::Yoke<str::Lines<'static>, String> to be used as an owned version of str::Lines.

My understanding is that the underlying cause of str::Lines<'a> being invariant over 'a instead of covariant over 'a despite only holding an &'a str is a limitation of the current implementation of string patterns, #27721.

Meta

rustc --version --verbose:

rustc 1.99.0-nightly (12c36e253 2026-08-10)
binary: rustc
commit-hash: 12c36e2539c54397c51d6ea4401defd8768a4f5b
commit-date: 2026-08-10
host: x86_64-unknown-linux-gnu
release: 1.99.0-nightly
LLVM version: 23.1.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal reproducer shown in src/lib.rs and investigate the string-pattern limitation referenced by issue #27721. Verify the variance behavior of str::Lines<'a> and determine whether the reported function can compile without weakening safety; done means the reproducer accepts the stated 'b: 'a relationship.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.