Skip to content

Suggest removal of type ascription in stable #95014

Closed
@estebank

Description

@estebank

Given the following code:

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
        .filter(|l |capital_letter (l)): impl Iterator<Itm = &str>
        .collect();

The current output is:

error: casts cannot be followed by a method call
 [--> src/main.rs:4:30
](https://play.rust-lang.org/#)  |
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
](https://play.rust-lang.org/#)  |
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
](https://play.rust-lang.org/#)  |
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>)
  |

It should (instead?/also?) suggest removing the type ascription, as it is more likely what the end user wanted. Particularly in stable compilers.

Taken from https://users.rust-lang.org/t/cannot-compile-my-code/72894

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.F-type_ascription`#![feature(type_ascription)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions