I am trying to make this conversion (Option<Option<String>> to Option<Option<&str>>) possible, but I still failed after trying many methods including using .map.
I know that the conversion is possible, however, if there's no nested Option (i.e. Option<String> to Option<&str>): just using .as_deref().
Using .map(|inner| inner.as_deref()) violates the ownership.
The reason using a plain
mapviolates ownership rules is because it takesself. You're consuming the outerOptionand then trying to borrow something from inside it, which doesn't work.However, there's an easy approach here, using
as_refand thenas_derefinside themap.as_refturns&Option<T>intoOption<&T>, whereTisOption<String>, so when you callmap, you get an&Option<String>, which you can then safely pass toas_deref.