How to use struct self in member method closure

后端 未结 3 509
抹茶落季
抹茶落季 2020-11-28 16:12

How can I call a method in closure? get_access_token method can set new access token based on self.get_base_url():

fn fetch_access_         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 16:43

    The closure passed to the get_or_insert_with method in Option is of type FnOnce - it thus consumes or moves the captured variables. In this case self is captured because of the usage of self.get_base_url() in the closure. However, since self is already borrowed, the closure cannot consume or move the value of self for unique access.

    This can be circumvented by using the get_or_insert method, but it will require you to perform the potentially expensive operation of fetching the access token every time get_access_token is called regardless of whether access_token is None or not.

提交回复
热议问题