Skip to content

Implemented Mul operator trait can't be used #22099

Closed
@bluss

Description

@bluss

A user provided the following test case that fails compiling at the point where the * operator is used.

use std::ops::Mul;

#[derive(Copy)]
pub struct S {
    a: f64
}

impl Mul<f64> for S {
    type Output = S;

    fn mul(self, scalar: f64) -> S {
        S {
            a: self.a * scalar
        }
    }
}

impl Mul<S> for f64 {
    type Output = S;

    fn mul(self, s: S) -> S {
        s.mul(self)
    }
}

fn main() {
    let s = S {a: 0f64};
    s * 5f64;
    5f64 * s; // error: mismatched types: expected `f64`, found `S`
              // (expected f64, found struct `S`) [E0308]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions