3 ms·
Interesting. I'm coming from Java that looks-like an object to me. I assume method dispatch here is based on the "receiver" as well right? What differentiates i
by kjeetgill 8y ago
Interesting. I'm coming from Java that looks-like an object to me. I assume method dispatch here is based on the "receiver" as well right? What differentiates it from an object? Not having identity? or Not being self describing (like a.getClass()) maybe?
Thank you for the insight.
- steveklabnik 8y agoNo problem! Syntax is just syntax. Imagine a function that takes two 32-bit numbers, and adds them together: fn foo(x: i32, y: i32) -> i32 { x + y } You'd call it like this: foo(5, 6) // would give back 11 However, this is just syntax. There's no reason this can't be 5.foo(6) It's purely a different way of calling the same thing. The first argument goes before the dot. In some languages, this is 100% interchangable. In Rust, it's not 100%, but it's pretty true. For example, the wrapping_add method adds two numbers together, wrapping around if the number is too large. These are equivalent: let x: i32 = 5; assert_eq!(x.wrapping_add(5), 10); assert_eq!(i32::wrapping_add(x, 5), 10); You can use either method or function call style. These aren't objects, they're just primitive 32 bit numbers. There's no heap allocation, there's no extra bookeeping info. There are some more details, but that's the basic idea. Does that make sense?