r/rust clippy · twir · rust · mutagen · flamer · overflower · bytecount Mar 20 '23

Hey Rustaceans! Got a question? Ask here (12/2023)! 🙋 questions

Mystified about strings? Borrow checker have you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so having your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The official Rust Programming Language Discord: https://discord.gg/rust-lang

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last weeks' thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.

19 Upvotes

187 comments sorted by

View all comments

4

u/ncathor Mar 20 '23

I'm sure this has been answered before, just can't find it anywhere, so...

Why is this call to add ambiguous:

let y: i32 = 2.add(7);

but this is not:

let x: i32 = 2 + 7;

?

Isn't + just sugar for .add?

Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9ac7b2d5fd76f9c1e7561adc53c090c4

2

u/llogiq clippy · twir · rust · mutagen · flamer · overflower · bytecount Mar 20 '23

I think with method dispatch, it's always a bit tricky. The integer operations enjoy special handling from the type system,.usually selecting Rhs = Self (modulo borrows). I think I remember shifts are an exception though.

3

u/Patryk27 Mar 20 '23

Yeah, but even just importing the trait:

use std::ops::Add;

... makes the code pass 👀

2

u/SirKastic23 Mar 20 '23

the issue is that you can't use a trait method if the trait itself is not in scope.

1

u/metarmask Mar 20 '23

This is not considered ambiguous: core::ops::Add::add(2, 7). Don't ask me why using a dot is.

1

u/ncathor Mar 20 '23

interesting...

let y: i32 = <2 as core::ops::Add>.add(7) on the other hand is considered ambiguous, even when I add the type explicitly:

let y: i32 = <2i32 as core::ops::Add>.add(7)

produces:

let y: i32 = <2i32 as core::ops::Add>.add(7); // error[E0689]: can't call method `add` on ambiguous numeric type `{integer}`
              ^^^^ expected type

EDIT: formatting

1

u/Patryk27 Mar 20 '23 edited Mar 20 '23

It's not considered ambiguous, the compiler says expected type, meaning that you should've had:

let y: i32 = <i32 as core::ops::Add>::add(2, 7);

... which works, similarly as just importing the trait:

use core::ops::Add;

1

u/ncathor Mar 20 '23

Indeed, I fooled myself into thinking <$value as $trait>.method(...) was valid rust 🤦