Some questions when reading the source code

2022-12-28_15-57

My problem is the e in the code, I can't find the square function? So I want to ask what syntax is there in rust to explain this code.

In vscode, when I put the mouse on square and e, it prompts me respectively: {unknown}, mut e: {unknown}

If the IDE doesn't know the type, then find it out yourself. Search for the map function in the documentation. There are a couple of results, but the relevant one here is obviously Option::map(). If you click on it, you will be directed to its full signature, from which it is apparent that Option::<T>::map() passes a T (i.e., the wrapped type itself) to the closure.

I know this map is a function of Option, what I want to know is where is the "square" function here, I can't find it in the code. According to the comments here, it should be the square of x, but I can't find the code that performs this operation. Is there a function for square in the standard library?

When just reading the code (and not using IDE, otherwise it's possible to use "Go to definition"), there are three things which should be considered:

  • The type of the value in question (here, e).
  • Full list of traits in current file.
  • Full list of the use statements present (actually, we need only traits, but syntactically they're not different).

After that, there are two possibilities:

  • Method might be an inherent method of type, in which case it'll appear in the docs for that type.
  • Method might appear in one of the traits present, in which case it'll appear in the documentation for either the trait or the type, depending on who was the implementor.
1 Like

i use search and find that square in function.rs,this file in .rustup/../..

thank you

My point was, if it's a method, you need to know the receiver type. There's not enough context in your question for me to tell what exactly the inner type is, but the square() function will be a method on whatever that type is.