Ambiguous associated type error

Hi there, I have the following code

// Main struct for Solver
pub struct Solver<'a> {
    pub problem: &'a Problem,
    pub method: String

// Functions for Solver
impl Solver<'_> {
    pub fn run (&self) {
        // Print
        println!("Solving in Solver");

        let mut optimiser =  match self.method.as_str() {
            "GeneticAlgorithm" => GeneticAlgorithm::new {
                    problem: self.problem,     
            _ => panic!()

pub struct GeneticAlgorithm<'a> {
    pub problem: &'a Problem,
    initialization: Initialization,
    termination: Termination,
    is_initialized: bool


impl<'a> GeneticAlgorithm<'a> {
    fn new(problem: &'a Problem) -> Self {
        Self {
            problem: problem,
            initialization: Initialization {
                sampling: 3,
                repair: 3,
                eliminate_duplicates: 3
            termination: Termination::new(),
            is_initialized: false


where I am trying to create a solver using struct. The solver requires parameters such as initialization and termination but I only want to pass the problem, and want the other parameters to take default values. The error i get is

error[E0223]: ambiguous associated type
  --> src/
18 |             "GeneticAlgorithm" => GeneticAlgorithm::new {
   |                                   ^^^^^^^^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<GeneticAlgorithm<'_> as Trait>::new`

but I cannot seem to work out how to overcome my issue. Any help would be greatly appreciated!

You have mixed up struct literal syntax and function call syntax. This is valid syntax for a struct literal (though missing fields):

GeneticAlgorithm {
    problem: self.problem,     

and this is valid syntax for a function call to your GenericAlgorithm::new function, which looks like it'll do what you want:


but GenericAlgorithm::new { would only be valid if new were a type of a struct (which is why the compiler mentioned 'associated type'), not a function.

(Rust has no “named arguments” for functions.)

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.