A confusing error[E0277], wrong generic bounds? Need helps

⚓ Rust    📅 2026-03-12    👤 surdeus    👁️ 6      

surdeus

use std::borrow::Cow;

fn case<'a, S>(_: S)
where
    Cow<'a, str>: From<S>,
    S: AsRef<str>,
{
}

fn test0<'a, I, S>(_: I)
where
    I: IntoIterator<Item = S>,
    Cow<'a, str>: From<S>,
{
    case("test");
}

fn test1() {
    case("test");
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `S: AsRef<str>` is not satisfied
  --> src/lib.rs:15:10
   |
15 |     case("test");
   |     ---- ^^^^^^ the trait `AsRef<str>` is not implemented for `S`
   |     |
   |     required by a bound introduced by this call
   |
note: required by a bound in `case`
  --> src/lib.rs:6:8
   |
 3 | fn case<'a, S>(_: S)
   |    ---- required by a bound in this function
...
 6 |     S: AsRef<str>,
   |        ^^^^^^^^^^ required by this bound in `case`
help: consider further restricting type parameter `S` with trait `AsRef`
   |
13 |     Cow<'a, str>: From<S>, S: std::convert::AsRef<str>
   |                            +++++++++++++++++++++++++++

error[E0308]: mismatched types
  --> src/lib.rs:15:10
   |
10 | fn test0<'a, I, S>(_: I)
   |                 - expected this type parameter
...
15 |     case("test");
   |     ---- ^^^^^^ expected type parameter `S`, found `&str`
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected type parameter `S`
                   found reference `&'static str`
note: function defined here
  --> src/lib.rs:3:4
   |
 3 | fn case<'a, S>(_: S)
   |    ^^^^        ----

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to 2 previous errors

2 posts - 2 participants

Read full topic

🏷️ Rust_feed