Cannot infer type in test from section 12.4 of rust book v2
Hello everyone! I'm learning Rust now using this book and faced strange(as for me) error on section 12.4 when running tests.
You can reproduce a bug with next snippet of code
pub fn search<'a> (q: &str, text: &'a str) -> Vec<&'a str> {
text.lines().filter(|line| line.contains(q)).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_compile_but_it_doesnt () {
let q = "queryABCD";
let t = "text with query";
assert_eq!( vec![], search(q, t) );
}
}
// error[E0282]: type annotations needed
// --> src/lib.rs:13:21
// |
// 13 | assert_eq!( vec![], search(q, t) );
// | ^^^^^^ cannot infer type for `T`
// |
// = note: this error originates in a macro outside of the current crate
However, it's fairly obvious that compiler knows the type annotation that must be infered from vec![] macro. Next snippet makes it clear
pub fn search<'a> (q: &str, text: &'a str) -> Vec<&'a str> {
text.lines().filter(|line| line.contains(q)).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_compile_but_it_doesnt () {
let q = "queryABCD";
let t = "text with query";
assert_eq!( 333, search(q, t) );
}
}
// error[E0277]: the trait bound `{integer}: std::cmp::PartialEq<std::vec::Vec<&str>>` is not satisfied
// --> src/lib.rs:13:9
// |
// 13 | assert_eq!( 333, search(q, t) );
// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't compare `{integer}` with `std::vec::Vec<&str>`
// |
// = help: the trait `std::cmp::PartialEq<std::vec::Vec<&str>>` is not implemented for `{integer}`
// = note: this error originates in a macro outside of the current crate
I think it's a bug but not sure since I'm novice in rust. I tried different toolchains, specifically 1.16, 1.18, 1.20(stable) and nightly and got the same result.
Hello everyone! I'm learning Rust now using this book and faced strange(as for me) error on section 12.4 when running tests.
You can reproduce a bug with next snippet of code
pub fn search<'a> (q: &str, text: &'a str) -> Vec<&'a str> { text.lines().filter(|line| line.contains(q)).collect() } #[cfg(test)] mod tests { use super::*; #[test] fn it_should_compile_but_it_doesnt () { let q = "queryABCD"; let t = "text with query"; assert_eq!( vec![], search(q, t) ); } } // error[E0282]: type annotations needed // --> src/lib.rs:13:21 // | // 13 | assert_eq!( vec![], search(q, t) ); // | ^^^^^^ cannot infer type for `T` // | // = note: this error originates in a macro outside of the current crateHowever, it's fairly obvious that compiler knows the type annotation that must be infered from vec![] macro. Next snippet makes it clear
pub fn search<'a> (q: &str, text: &'a str) -> Vec<&'a str> { text.lines().filter(|line| line.contains(q)).collect() } #[cfg(test)] mod tests { use super::*; #[test] fn it_should_compile_but_it_doesnt () { let q = "queryABCD"; let t = "text with query"; assert_eq!( 333, search(q, t) ); } } // error[E0277]: the trait bound `{integer}: std::cmp::PartialEq<std::vec::Vec<&str>>` is not satisfied // --> src/lib.rs:13:9 // | // 13 | assert_eq!( 333, search(q, t) ); // | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't compare `{integer}` with `std::vec::Vec<&str>` // | // = help: the trait `std::cmp::PartialEq<std::vec::Vec<&str>>` is not implemented for `{integer}` // = note: this error originates in a macro outside of the current crateI think it's a bug but not sure since I'm novice in rust. I tried different toolchains, specifically 1.16, 1.18, 1.20(stable) and nightly and got the same result.
Esteban Kuber at 2019-10-29 01:25:58
I think this is expected behaviour, but maybe the error could be better.
The problem is that while the compiler knows that you want to compare something to
Vec<&str>, it doesn't know what exactly: You can, for example, compare both aVec<String>andVec<&str>.Tim Neumann at 2017-10-10 17:25:10
Thanks for your reply, I got the idea. Should I file a bug elsewhere for documentation team to fix their code in problematic section?
Deleted user at 2017-10-10 18:03:37
Current output
error[E0282]: type annotations needed --> src/lib.rs:13:21 | 13 | assert_eq!( vec![], search(q, t) ); | ^^^^^^ cannot infer type of the type parameter `T` declared on the struct `Vec` | = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0283]: type annotations needed --> src/lib.rs:13:21 | 13 | assert_eq!( vec![], search(q, t) ); | ------------^^^^^^---------------- | | | | | cannot infer type of the type parameter `T` declared on the struct `Vec` | type must be known at this point | = note: multiple `impl`s satisfying `_: PartialEq<&str>` found in the following crates: `alloc`, `core`, `std`: - impl<'a, 'b> PartialEq<&'a str> for String; - impl<'a, 'b> PartialEq<&'b str> for Cow<'a, str>; - impl<> PartialEq<&str> for OsString; - impl<A, B> PartialEq<&B> for &A where A: PartialEq<B>, A: ?Sized, B: ?Sized; - impl<A, B> PartialEq<&B> for &mut A where A: PartialEq<B>, A: ?Sized, B: ?Sized; = note: required for `Vec<_>` to implement `PartialEq<Vec<&str>>` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)Esteban Kuber at 2022-12-28 03:17:30