Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
816 views
in Technique[技术] by (71.8m points)

rust - Understanding (automatic?) Deref/coercion when adding references and values of numbers

How to make sense of the following piece of code? I'm new to Rust but have background on C/Haskell and a little bit C++. The only reference I can find is to deref coercions.

fn main() {
    let xs: [u32; 4] = [0, 1, 2, 3];
    let mut i: u32 = 0;
    for x in xs.iter() {
        if i > *x {     // It looks x is an iterator. Understood.            
            i = i + x;  // no error. (coerced)
                        //Quote: "Rust will do this as many times
                        //       as possible until the types match."
            i = i + *x; // no error (explicit deref)
            i += x;     // error about u32/&u32 mismatch. Why the magic failed?
            i += *x;    // no error (explicit deref)
        }
    }
    println!("{}", i);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no auto-deref or coercion here, i + x works simply because u32 implements both Add<u32> and Add<&u32>. If you check the docs for u32, you'll find the following four trait impls:

impl Add<u32> for u32
impl<'a> Add<u32> for &'a u32
impl<'a> Add<&'a u32> for u32
impl<'a, 'b> Add<&'a u32> for &'b u32

u32 only implements AddAssign<u32> but not AddAssign<&u32> (this is a bug and will be fixed in 1.18 or 1.19 fixing it causes regression which means this impl probably needs to wait for Rust 2.0), so i += x is an error.

impl AddAssign<u32> for u32
//impl<'a> AddAssign<&'a u32> for u32 <-- is missing.

Why does auto-dereferencing not happen? — Auto-deref only happens when it is a receiver i.e. the "self" in a method call foo.bar(). x is not a "self" argument and + is not a method call. So there's no auto-deref here. See What are Rust's exact auto-dereferencing rules? for detail.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...