Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generics1: does not require use of explicit generics #1448

Open
benjaminbauer opened this issue Mar 28, 2023 · 5 comments · May be fixed by #1470
Open

generics1: does not require use of explicit generics #1448

benjaminbauer opened this issue Mar 28, 2023 · 5 comments · May be fixed by #1470

Comments

@benjaminbauer
Copy link

The idiomatic solution for generics1 is to simply remove the explicit type from the variable binding since it can be inferred:

fn main() {
    let mut shopping_list = Vec::new();
    shopping_list.push("milk");
}

The hint says:

Vectors in Rust make use of generics to create dynamically sized arrays of any type.
You need to tell the compiler what type we are pushing onto this vector.

Two problems with this:

  1. The solution is better without specifying a type and thus is not teaching/requiring the explicit use of generics
  2. The hint is wrong, because there is not need to tell the compiler the type
@Eshanatnight
Copy link

I agree with you. Maybe the thought process at the time of writing was

  • as a way of learning about generics without depending much on the compiler, or better phrased as to get the hang of the generic syntax
let mut shopping_list: Vec<&str> = Vec::new();
  • and going of the reasoning, making one familiar to the explicit syntax, would make one more comfortable with the turbofish syntax in the future

Again, I do agree with you, but this is the conclusion I drew after reading generics1.rs.

@benjaminbauer
Copy link
Author

I see two options here:

  1. Make it explicit, that this example is not showing the idiomatic solution
  2. Find a better example, where the use of generics is required. The challenge here is to find one that is simple enough, to not distract from the point being made

@shadows-withal
Copy link
Member

I'd lean towards the latter option myself.

@poneciak57 poneciak57 linked a pull request Apr 5, 2023 that will close this issue
@Eshanatnight
Copy link

Find a better example, where the use of generics is required. The challenge here is to find one that is simple enough, to not distract from the point being made

I agree with this too

@Eshanatnight
Copy link

Hi, I found this example of generics and the turbofish operator.

// generics
fn get_default_value<T>() -> T
where
    T: Default,
{
    Default::default()
}

fn main() {
    let number_default_value = get_default_value::<i32>();
    println!("Default value of i32: {}", number_default_value);
}

I would like to get you guys's opinions. I'm not sure on this because it uses the where clause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants