Cannot compile my code

rustc 1.59.0 (9d1b2106e 2022-02-23) on win11-64 (I am beginner)

The code is from
https://youtu.be/xNrglKGi-7o?t=337

use std::fs::read_to_string;
fn main() {
   let input: String = read_to_string ("file.txt").unwrap() ;
    let good_lines: Vec<_> = input: String  
        .lines(): Lines           //std::io::Lines;    //std::str::Lines
        .filter(|l |capital_letter (l)): impl Iterator<Itm = &str>
        .collect();
    dbg!(good_lines);
}
#[allow(unused)]
fn capital_letter(s: &str)->bool{
    matches!(s.chars().next(),Some(c)if c.is_uppercase()  )
}

/////////////////////////

casts cannot be followed by a method call
cannot find type `Lines` in this scope
`impl Trait` not allowed outside of function and method return types

/////////////////////////

Compiling file v0.1.0 (file)
error: casts cannot be followed by a method call
 --> src\main.rs:4:30
  |
4 |     let good_lines: Vec<_> = input: String  
  |                              ^^^^^^^^^^^^^
  |
help: try surrounding the expression in parentheses
  |
4 |     let good_lines: Vec<_> = (input: String)
  |                              +             +

error: casts cannot be followed by a method call
 --> src\main.rs:4:30
  |
4 |       let good_lines: Vec<_> = input: String
  |  ______________________________^
5 | |         .lines(): Lines
  | |_______________________^
  |
help: try surrounding the expression in parentheses
  |
4 ~     let good_lines: Vec<_> = (input: String  
5 ~         .lines(): Lines)
  |

error: casts cannot be followed by a method call
 --> src\main.rs:4:30
  |
4 |       let good_lines: Vec<_> = input: String
  |  ______________________________^
5 | |         .lines(): Lines
6 | |         .filter(|l |capital_letter (l)): impl Iterator<Itm = &str>      
  | |__________________________________________________________________^
  |
help: try surrounding the expression in parentheses
  |
4 ~     let good_lines: Vec<_> = (input: String
5 |         .lines(): Lines
6 ~         .filter(|l |capital_letter (l)): impl Iterator<Itm = &str>)       
  |

error[E0412]: cannot find type `Lines` in this scope
 --> src\main.rs:5:19
  |
5 |         .lines(): Lines
  |                   ^^^^^ expecting a type here because of type ascription  
  |
help: consider importing one of these items
  |
1 | use core::str::Lines;
  |
1 | use std::io::Lines;
  |
1 | use std::str::Lines;
  |

error[E0562]: `impl Trait` not allowed outside of function and method return types
 --> src\main.rs:6:42
  |
6 |         .filter(|l |capital_letter (l)): impl Iterator<Itm = &str>        
  |                                          ^^^^^^^^^^^^^^^^^^^^^^^^^        

Some errors have detailed explanations: E0412, E0562.
For more information about an error, try `rustc --explain E0412`.
error: could not compile `file` due to 5 previous errors
The terminal process " cargo.exe 'run', '--package', 'file', '--bin', 'file'" terminated with exit code: 101.

Please format your code and error output. See Forum Code Formatting and Syntax Highlighting for details.

You are probably using hints from IDE as part of code - in video they are shown in dark grey.

3 Likes

@Cerber-Ursi is describing the problem correctly.

The inlay type hints that you need to remove from the code are as follows:

use std::fs::read_to_string;
fn main() {
   let input: String = read_to_string ("file.txt").unwrap() ;
-   let good_lines: Vec<_> = input: String  
+   let good_lines: Vec<_> = input
-       .lines(): Lines           //std::io::Lines;    //std::str::Lines
+       .lines()
-       .filter(|l |capital_letter (l)): impl Iterator<Itm = &str>
+       .filter(|l |capital_letter (l))
        .collect();
    dbg!(good_lines);
}
#[allow(unused)]
fn capital_letter(s: &str)->bool{
    matches!(s.chars().next(),Some(c)if c.is_uppercase()  )
}

P.S. I have edited your post for you so the code is in code blocks.

1 Like

!!! It seems so. Thank You all so much.
(Additionally my eng is not native.. It was hard to understand about hints )

Again... What is wrong?

Compiling file v0.1.0 (file)
error[E0425]: cannot find value ggg in this scope
--> src\main.rs:15:26

https://youtu.be/xNrglKGi-7o?t=1143 (at 19 min)

use std::{fs::read_to_string,time::Duration};

#[derive(Debug)]

struct S<'a>{

input8: String,

ggg: Vec<&'a str>,

}

#[allow(unused)]

fn capital_letter(s: &str)->bool{ matches!(s.chars().next(),Some(c) if c.is_uppercase() )}

fn main() {

/**************** 8 **********************/

let input8: String = read_to_string ("file.txt").unwrap() ;

let mut s = S {input8, ggg , };

s.ggg=s.input8.lines() .filter(|l|capital_letter (l)).collect();

dbg!(&s);

/************** 9 ************************/

}

Formatted and fixed:


use std::{fs::read_to_string, time::Duration};

#[derive(Debug)]

struct S<'a> {
    input8: String,

    ggg: Vec<&'a str>,
}

#[allow(unused)]

fn capital_letter(s: &str) -> bool {
    matches!(s.chars().next(),Some(c) if c.is_uppercase() )
}

fn main() {
    /**************** 8 **********************/

    let input8: String = read_to_string("file.txt").unwrap();
    //                 The vec![] call was missing
    //                             👇️                                       
    let mut s = S { input8, ggg: vec![] };

    s.ggg = s.input8.lines().filter(|l| capital_letter(l)).collect();

    dbg!(&s);

    /************** 9 ************************/
}

(playground)

If you're new to rust, you might want to start with some easier learning material. The linked video seems to be about self-referential structs, which are quite an advanced topic.
I'd recommend having a look here for learning resources and can heavily recommend the book :blush:

2 Likes

Thank You.. I agree with You. It is true. But he started with very simple and useful for beginners things:

let input: String = read_to_string ("file.txt").unwrap() ;
println!("{}", input);  
/////////////// 1 /////////////// 
for line in  input.lines()     {   if  capital_letter (line)  {println!("- {}", line); }                }

/////////////// 2 ///////////////
for line in input.lines(). filter(|l |capital_letter (l)) { println!("+ {}", line); }
/////////////// 3 ///////////////

Now I am watching
Introduction to Rust Part 1
https://youtu.be/WnWGO-tLtLA?t=1549