Skip to content

question idea: parentheses-dependent code #66

@rodrimati1992

Description

@rodrimati1992

Credit goes to helix65535 (on the Rust Programming Language Community server)

This is code that does two different things depending purely on parenthetization:

use std::ops::{Sub, Neg};

struct Evil;

impl Sub<Evil> for () {
    type Output = &'static str;
    fn sub(self, _: Evil) -> &'static str {
        "sub"
    }
}

impl Neg for Evil {
    type Output = &'static str;
    fn neg(self) -> &'static str {
        "neg"
    }
}

fn main() {
    println!("{}", {
        loop { break; } - Evil
    });
    println!("{}", {(
        loop { break; } - Evil
    )});
}

it prints

neg
sub

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions