0

Kind of a weird one, but as I was programming a Rust application, I found that weird case that compiles but it's not clear to me why it does:

use std::thread;
use std::sync::Arc;

pub fn main() {
    let vec = Arc::new(vec![1, 2, 3]);
    
    for _i in 0..10 {
        let var = Arc::clone(&vec);
        thread::spawn(move || test(&var));
    }
}

fn test(var: &Vec<i32>) {
    println!("{var:?}");
}

So here we pass var to the test function as a &Arc<Vec<>> (rust-analyzer confirms that), and even though the function is expecting a &Vec<>, this works.

Now this implies that &Vec and &Arc<Vec> are equivalent, which is super weird to me (especially since that doesn't seem to work with Vec and Arc<Vec>)

I thought that maybe since Arc is a reference counter, it can be represented as & as syntaxic sugar, but surely that's not it since the same maneuver doesn't compile with &Vec<> and Arc<Vec>.

Does that mean that Arcs can be implicit, but only in certain conditions? This is my leading theory because Arcs let the user access the data without getting in the way but that still looks weird to me, because we aren't getting the information that the reference is being counted.

If one of you could explain to me why a reference to a reference counter that points to a vector is equivalent to a reference to a vector, it would be much appreciated!

Edit: So a commenter (thank you PitaJ) pointed out that a deref was happening. I'm still curious about losing that Arc qualifier.

The testing I did before posting seemed to show that the reference counter is still working (which is expected, I didn't think that there was a bug in Rust), so that means that Arc still does its thing even after it derefs to its inner value?

Alice
  • 972
  • 1
  • 7
  • 17
  • 5
    They aren't equivalent, but `&Arc>` can be coerced into `&Vec<_>` via a deref coercion. https://doc.rust-lang.org/reference/type-coercions.html#coercion-types – PitaJ Jun 12 '23 at 17:37
  • @PitaJ Thanks a lot for the link! – Alice Jun 12 '23 at 17:50
  • It works the same way as when you pass `&String` to a function that expects `&str`, or `&Box` to a function that expects `&T`. – Aleksander Krauze Jun 12 '23 at 18:06
  • @cdhowie not really, it's the same subject matter but not the same question. What answers my question is the first comment ! – Alice Jun 12 '23 at 18:13
  • 1
    @AleksanderKrauze right! I was wondering about the &str case as well but I figured it was some weird String stuff. Happy to have learned what Derefs are here! – Alice Jun 12 '23 at 18:15
  • 2
    Related: [What is the relation between auto-dereferencing and deref coercion?](https://stackoverflow.com/questions/53341819/what-is-the-relation-between-auto-dereferencing-and-deref-coercion) – Sven Marnach Jun 12 '23 at 19:56
  • 1
    Can the people who keep submitting the same question as a duplicate read my edit and/or the post in general ? Or are we just not allowed to ask a question related to a subject that is discussed in another question ?????? – Alice Jun 12 '23 at 19:58
  • Oh, thank you @SvenMarnach, that's an interesting read ! – Alice Jun 12 '23 at 20:02
  • @Alice your question being marked as duplicate doesn't mean that it was wrong to ask it, or that the question is the same. It just means that the answer is the same, because `&Arc: Deref` and the mechanism that you are asking about is deref coercion. – jthulhu Jun 13 '23 at 05:48
  • @jthulhu Ok thanks for clarifying it, it very much had a negative connotation to me. I still don't agree that that qualifies as a duplicate because none of the answers in the duplicate answers my question: it's just people who know what they're talking about discussing a thing I had no idea existed ! And even the title of the question assumes knowledge of something I didn't know. Honestly if I had just gotten the question closed with this duplicate linked I would have just given up on answering it. Maybe this is just how duplicate works but that makes for a bad exp. for inexperienced ppl – Alice Jun 13 '23 at 08:58
  • I doesn't really matter because some helpful people in the comments have linked some great resources ! But yea i'm a bit confused – Alice Jun 13 '23 at 09:00
  • 1
    @Alice usually, negative feedback in SO is either negative reputation or a warning that you will receive some kind of punishment if you don't improve your behavior. Btw, since your question has been closed for being a duplicate, you can edit the question to explain that you have acknowledge the existence of the other question, and explain why you don't think it is a duplicate (ie. what is lacking in the answer of the other question to fully answer your question), maybe rephrase it so it's clear what you expect as an answer. Then, the question can be reopened. – jthulhu Jun 13 '23 at 19:24
  • Also, if the question doesn't get reopened, you can always post a question on the meta forum saying something like "I don't understand why my question has been closed", and people will either come to see your question, agree with you, and vote to reopen, and give you more detailed instructions on how to reopen your question. SO is not very beginner-friendly but, unless your question has severe content problems, it's not that hard to reopen it once you know how to do that. – jthulhu Jun 13 '23 at 19:26
  • @jthulhu I see, thank you for the messages! Hm I think I'll just post a new one if I ever get around to it, but thanks for the guidance – Alice Jun 15 '23 at 08:06

0 Answers0