Skip to main content
Post Closed as "Duplicate" by Shepmaster rust
deleted 319 characters in body; edited title
Source Link
Shepmaster
  • 439.3k
  • 116
  • 1.3k
  • 1.5k

Rust String::contains(String) gives compiler error"the trait bound String: FnMut<(char,)>` is not satisfied"

The following code tries to check whether a Rust String contains another Rust String. I get a compiler error when I use the String::contains method. One wouldI expect String::contains(String) would work straight out of the box. 

What is the correct way to do this in Rust, when the pattern being searched for is not a string literal? I did the rustc --explain E0277, and it seems like String does not implement athe Pattern trait, is that true?

Code below is courtesy BHustus. Thanks for an MCVE.

fn main() { let a: String = String::from("abcdefgh"); let b: String = String::from("def"); if a.contains(b) { println!("Contained"); } else { println!("Not contained"); } } 

The compiler error:

Compiling playground v0.0.1 (file:///playground) error[E0277]: the trait bound `std::string::String:   std::ops::FnMut<(char,)>` is not satisfied --> src/main.rs:56:10 | 56 | if a.contains(b) { | ^^^^^^^^ the trait `std::ops::FnMut<(char,)>` is not   implemented for `std::string::String` | = note: required because of the requirements on the impl of   `std::str::pattern::Pattern<'_>` for `std::string::String` error: aborting due to previous error For more information about this error, try `rustc --explain E0277`. error: Could not compile `playground`. 

Rust String::contains(String) gives compiler error

The following code tries to check whether a Rust String contains another Rust String. I get a compiler error when I use the String::contains method. One would expect String::contains(String) would work straight out of the box. What is the correct way to do this in Rust, when the pattern being searched for is not a string literal? I did the rustc --explain E0277, and it seems like String does not implement a Pattern trait, is that true?

Code below is courtesy BHustus. Thanks for an MCVE.

fn main() { let a: String = String::from("abcdefgh"); let b: String = String::from("def"); if a.contains(b) { println!("Contained"); } else { println!("Not contained"); } } 

The compiler error:

Compiling playground v0.0.1 (file:///playground) error[E0277]: the trait bound `std::string::String:   std::ops::FnMut<(char,)>` is not satisfied --> src/main.rs:5:10 | 5 | if a.contains(b) { | ^^^^^^^^ the trait `std::ops::FnMut<(char,)>` is not   implemented for `std::string::String` | = note: required because of the requirements on the impl of   `std::str::pattern::Pattern<'_>` for `std::string::String` error: aborting due to previous error For more information about this error, try `rustc --explain E0277`. error: Could not compile `playground`. 

String::contains(String) gives "the trait bound String: FnMut<(char,)>` is not satisfied"

The following code tries to check whether a String contains another String. I get a compiler error when I use the String::contains method. I expect String::contains(String) would work straight out of the box. 

What is the correct way to do this in Rust, when the pattern being searched for is not a string literal? I did rustc --explain E0277, and it seems like String does not implement the Pattern trait, is that true?

fn main() { let a = String::from("abcdefgh"); let b = String::from("def"); if a.contains(b) { println!("Contained"); } else { println!("Not contained"); } } 

The compiler error:

error[E0277]: the trait bound `std::string::String: std::ops::FnMut<(char,)>` is not satisfied --> src/main.rs:6:10 | 6 | if a.contains(b) { | ^^^^^^^^ the trait `std::ops::FnMut<(char,)>` is not implemented for `std::string::String` | = note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `std::string::String` 
formatting and highlighting of error message
Source Link
Lukas Kalbertodt
  • 90.8k
  • 32
  • 278
  • 338

The following code tries to check whether a Rust String contains another Rust String. I get a compiler error when I use the String::contains method. One would expect String::contains(String) would work straight out of the box. What is the correct way to do this in Rust, when the pattern being searched for is not a string literal? I did the rustc --explain E0277, and it seems like String does not implement a Pattern trait, is that true?

Code below is courtesy BHustus. Thanks for an MCVE.

fn main() { let a: String = String::from("abcdefgh"); let b: String = String::from("def"); if a.contains(b) { println!("Contained"); } else { println!("Not contained"); } } 

The compiler error:

Compiling playground v0.0.1 (file:///playground) error[E0277]: the trait bound `std::string::String: std::ops::FnMut<(char,)>` is not satisfied --> src/main.rs:5:10 | 5 | if a.contains(b) { | ^^^^^^^^ the trait `std::ops::FnMut<(char,)>` is not implemented for `std::string::String` | = note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `std::string::String` error: aborting due to previous error For more information about this error, try `rustc --explain E0277`. error: Could not compile `playground`. To learn more, run the command again with --verbose. The compiler error: 
Compiling playground v0.0.1 (file:///playground) error[E0277]: the trait bound `std::string::String: std::ops::FnMut<(char,)>` is not satisfied --> src/main.rs:5:10 | 5 | if a.contains(b) { | ^^^^^^^^ the trait `std::ops::FnMut<(char,)>` is not implemented for `std::string::String` | = note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `std::string::String` error: aborting due to previous error For more information about this error, try `rustc --explain E0277`. error: Could not compile `playground`. 

The following code tries to check whether a Rust String contains another Rust String. I get a compiler error when I use the String::contains method. One would expect String::contains(String) would work straight out of the box. What is the correct way to do this in Rust, when the pattern being searched for is not a string literal? I did the rustc --explain E0277, and it seems like String does not implement a Pattern trait, is that true?

Code below is courtesy BHustus. Thanks for an MCVE.

fn main() { let a: String = String::from("abcdefgh"); let b: String = String::from("def"); if a.contains(b) { println!("Contained"); } else { println!("Not contained"); } } 

The compiler error:

Compiling playground v0.0.1 (file:///playground) error[E0277]: the trait bound `std::string::String: std::ops::FnMut<(char,)>` is not satisfied --> src/main.rs:5:10 | 5 | if a.contains(b) { | ^^^^^^^^ the trait `std::ops::FnMut<(char,)>` is not implemented for `std::string::String` | = note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `std::string::String` error: aborting due to previous error For more information about this error, try `rustc --explain E0277`. error: Could not compile `playground`. To learn more, run the command again with --verbose. The compiler error: 

The following code tries to check whether a Rust String contains another Rust String. I get a compiler error when I use the String::contains method. One would expect String::contains(String) would work straight out of the box. What is the correct way to do this in Rust, when the pattern being searched for is not a string literal? I did the rustc --explain E0277, and it seems like String does not implement a Pattern trait, is that true?

Code below is courtesy BHustus. Thanks for an MCVE.

fn main() { let a: String = String::from("abcdefgh"); let b: String = String::from("def"); if a.contains(b) { println!("Contained"); } else { println!("Not contained"); } } 

The compiler error:

Compiling playground v0.0.1 (file:///playground) error[E0277]: the trait bound `std::string::String: std::ops::FnMut<(char,)>` is not satisfied --> src/main.rs:5:10 | 5 | if a.contains(b) { | ^^^^^^^^ the trait `std::ops::FnMut<(char,)>` is not implemented for `std::string::String` | = note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `std::string::String` error: aborting due to previous error For more information about this error, try `rustc --explain E0277`. error: Could not compile `playground`. 
Replaced original code with MCVE.
Source Link
vamsikal
  • 51
  • 1
  • 6

Code below is courtesy BHustus. Thanks for an MCVE.

use std::io; use std::io::Stdin; use std::str::FromStr; fn read_line(stdin : &Stdin, line : &mut String) { match stdin.read_linemain(line) { let a: String = OkString::from(_"abcdefgh") => return,;   Err(msg) => panic!("Could not read line!let Messageb: {}.", msg), } } fnString parse_t<T= String: FromStr>(s : &strfrom("def") -> T {;  matchif s.trim()a.parsecontains(b) { Ok(y) => y,   Err(_) => panicprintln!("Could not read from string!""Contained"),; } } fn read_t<T : FromStr>(stdin : &Stdin) -> Telse {   let mut line = String::new();  read_lineprintln!(stdin, &mut"Not linecontained"); parse_t(&line) }   fn print_can_sort_strings(strs : &mut Vec<(usize, String)>) { strs.sort();}  for i in

The compiler error:

Compiling 1playground v0.0.1 strs.len(file:///playground) {  let (_,error[E0277]: s_i_minus_1)the =trait strs[ibound -`std::string::String: 1];  let std::ops::FnMut<(_char, s_i) = strs[i]; >` is not //////////////////////////////////////////////////////satisfied   --> /src/ THIS STRINGmain.rs:5:CONTAINS CALL GIVES A COMPILER ERROR.10   //////////////////////////////////////////////////////|  5 | if s_ia.contains(s_i_minus_1b) {   // Do nothing ... }| else {  ^^^^^^^^ the trait println!`std::ops::FnMut<("NO"char,); return; } } >` is not println!("YES");  implemented for (_, s) in strs {`std::string::String`  println!("{}", s); | = note: } } fnrequired main()because { of the requirements on letthe stdinimpl =of io `std::stdin(); let n str: u64:pattern::Pattern<'_>` =for read_t(&stdin);`std::string::String`  let mut strs = vec![];  for _error: inaborting 0due ..to nprevious {error   For more information about this leterror, muttry s`rustc =--explain String::new();E0277`.  error: Could read_line(&stdin,not &mutcompile s);`playground`.   To learn more, run the command again strs.push((s.len(),with s.trim()--verbose.to_string()));  }   The print_can_sort_strings(&mutcompiler strs); }error: 

The compiler error:

vamsi@vamsi-laptop:~/learn/codeforces/901_1000/problem_988_b$ cargo build Compiling problem_988_b v0.1.0 (file:///home/vamsi/learn/codeforces/901_1000/problem_988_b) error[E0277]: the trait bound `std::string::String: std::ops::FnMut<(char,)>` is not satisfied --> src/main.rs:30:16 | 30 | if s_i.contains(s_i_minus_1) { | ^^^^^^^^ the trait `std::ops::FnMut<(char,)>` is not implemented for `std::string::String` | = note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `std::string::String` error: aborting due to previous error For more information about this error, try `rustc --explain E0277`. error: Could not compile `problem_988_b`. 
use std::io; use std::io::Stdin; use std::str::FromStr; fn read_line(stdin : &Stdin, line : &mut String) { match stdin.read_line(line) { Ok(_) => return,   Err(msg) => panic!("Could not read line! Message: {}.", msg), } } fn parse_t<T : FromStr>(s : &str) -> T { match s.trim().parse() { Ok(y) => y,   Err(_) => panic!("Could not read from string!"), } } fn read_t<T : FromStr>(stdin : &Stdin) -> T {   let mut line = String::new();  read_line(stdin, &mut line); parse_t(&line) }   fn print_can_sort_strings(strs : &mut Vec<(usize, String)>) { strs.sort();  for i in 1 .. strs.len() {  let (_, s_i_minus_1) = strs[i - 1];  let (_, s_i) = strs[i];  //////////////////////////////////////////////////////   // THIS STRING::CONTAINS CALL GIVES A COMPILER ERROR.   //////////////////////////////////////////////////////   if s_i.contains(s_i_minus_1) {   // Do nothing ... } else {  println!("NO"); return; } }  println!("YES");   for (_, s) in strs {  println!("{}", s); } } fn main() {  let stdin = io::stdin(); let n : u64 = read_t(&stdin);  let mut strs = vec![];  for _ in 0 .. n {   let mut s = String::new();   read_line(&stdin, &mut s);   strs.push((s.len(), s.trim().to_string()));  }    print_can_sort_strings(&mut strs); } 

The compiler error:

vamsi@vamsi-laptop:~/learn/codeforces/901_1000/problem_988_b$ cargo build Compiling problem_988_b v0.1.0 (file:///home/vamsi/learn/codeforces/901_1000/problem_988_b) error[E0277]: the trait bound `std::string::String: std::ops::FnMut<(char,)>` is not satisfied --> src/main.rs:30:16 | 30 | if s_i.contains(s_i_minus_1) { | ^^^^^^^^ the trait `std::ops::FnMut<(char,)>` is not implemented for `std::string::String` | = note: required because of the requirements on the impl of `std::str::pattern::Pattern<'_>` for `std::string::String` error: aborting due to previous error For more information about this error, try `rustc --explain E0277`. error: Could not compile `problem_988_b`. 

Code below is courtesy BHustus. Thanks for an MCVE.

fn main() { let a: String = String::from("abcdefgh"); let b: String = String::from("def");  if a.contains(b) { println!("Contained"); } else { println!("Not contained"); } } 

The compiler error:

Compiling playground v0.0.1 (file:///playground) error[E0277]: the trait bound `std::string::String: std::ops::FnMut<(char,)>` is not satisfied --> src/main.rs:5:10 | 5 | if a.contains(b) { | ^^^^^^^^ the trait `std::ops::FnMut<(char,)>` is not implemented for `std::string::String` | = note: required because of the requirements on the impl of  `std::str::pattern::Pattern<'_>` for `std::string::String` error: aborting due to previous error  For more information about this error, try `rustc --explain E0277`. error: Could not compile `playground`.  To learn more, run the command again with --verbose. The compiler error: 
formatting and highlighting
Source Link
Lukas Kalbertodt
  • 90.8k
  • 32
  • 278
  • 338
Loading
Source Link
vamsikal
  • 51
  • 1
  • 6
Loading