0

From here, I run the snippet code.

for _ in 0..10 {
    let counter = Arc::clone(&counter);
    let handle = thread::spawn(move || {
        let mut num = counter.lock().unwrap();

        *num += 1;
    });
    handles.push(handle);
}

When I type this sample code in an IDE, I have no idea what the type of num is. I event don't know to use * to dereference the value. But fortunately, the IDE helps me.

IDE hint the data type

The link provides a useful message about the type of num. I quote:

pointer called MutexGuard, wrapped in a LockResult that we handled with the call to unwrap. The MutexGuard smart pointer implements Deref to point at our inner data;

My wondering is: as a beginner, how can I know or infer the data type in Rust? If I don't got the message in the link or I don't get help from the IDE, How could I get a sense of the returned data type in Rust?

E_net4
  • 27,810
  • 13
  • 101
  • 139
gfan
  • 1,027
  • 1
  • 14
  • 28
  • 5
    Usually you know the function you're using and you look at its return type in the documentation. When you're unsure (because of traits for example), you can use the old internet tactic of pretending something to get the truth: try an assign (`let v:u8 = something`) then wait for the compiler to tell you that `something` isn't an `u8` and what it is. – Denys Séguret Nov 23 '21 at 07:53
  • I have dozen of doc tab open, that I close at the end of the day. That said you generally don't need to know the exact type you are dealing with if the api is well design – Stargateur Nov 23 '21 at 08:01
  • 1
    [quote](https://stackoverflow.com/a/21747400/1233251): _"If you merely wish to find out the type of a variable and are willing to do it at compile time, you can cause an error and get the compiler to pick it up."_ `let mut y: () = counter.lock().unwrap();` – E_net4 Nov 23 '21 at 09:32
  • Think of it that way. If you were saying: "As a beginner at flying planes, I can't tell what speed my aircraft is flying, the only way to do so is by looking at the speedometer in the cockpit. How can I find out what speed I'm flying at?". Well, there's no big secret. Even people who are seasoned developers are relying on the tooling to get the information. While the previous comments are very useful, the comfort brought by IDEs or advanced text editors cannot be overstated. – SirDarius Nov 23 '21 at 09:47
  • @SirDarius The question makes it perfectly clear that the OP is already aware of the IDE support and are right in asking about other ways to do it. They might be looking at a snippet of code in a github diff view, or in a book, or in a Slack conversation, or on an SSH connection where they don't have the IDE fired up. The question of how to best find the inferred type is a very valid one and should not be dismissed with "just use IDE", let alone with incorrect flying analogies. – user4815162342 Nov 23 '21 at 10:24
  • @user4815162342 I'm not dismissing anything. The previous comments provide great information, and I said so. Can't add more to that. The message I'm trying to get through here is tangential to the main question (hence why it's a comment and not an answer). That is for someone who don't consider themselves as a beginner, by experience, using existing tooling can be more comfortable than techniques such as causing compiler errors voluntarily. About analogies, there are never "correct", by virtue of being comparisons between closely or not related things. They are however a quite fun to write :) – SirDarius Nov 23 '21 at 10:58

0 Answers0