Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an Option<&T> to Option<T> [duplicate]

Tags:

generics

rust

I have a function doing that for a int:

fn some_to_value(src: Option<&int>) -> Option<int> {
    match src {
        Some(x) => Some(*x),
        None => None
    }
}

and I wanted to make it generic (and still use it with an "int" at the level of the caller). It would be ok for me if the instance of T is copied. So I tried with that:

fn some_to_value<T>(src: Option<&T>) -> Option<T> {
    match src {
        Some(x) => Some(*x),
        None => None
    }
}

I get:

error: cannot move out of dereference of `&`-pointer
Some(x) => Some(*x),
                ^~

I fail to understand why it fails (I'm a beginner).

Some context: I made a copy of an Option, because I realized that after doing a "find" on a "HashMap", the map will be immutable as long as the return of the "find" (an Option containing a reference to an item of the map) is alive.

like image 378
Fabimaru Avatar asked Jun 27 '14 20:06

Fabimaru


People also ask

What does convert options mean?

Conversion option refers to a clause that has to do with adjustable-rate mortgages (ARM) that enable an individual to change the adjustable-rate mortgage to fixed rates at a certain future date.

What is a conversion option strategy?

Conversion arbitrage in options is an arbitrage strategy that can be undertaken for the chance of a riskless profit when options are either theoretically overpriced or underpriced relative to each other and the underlying stock—as determined by the trader's pricing model.

How do you convert a call option to a put option?

Market Price > Strike Price = Out of Money put option = Gains / Profits. Market Price < Strike Price = In the Money put option = Loss. Market Price = Strike Price = At the Money call option = Profit in the form of premium.

What is convert option in Zerodha?

The Convert Position option in Zerodha is mainly to convert your trade from MIS (Margin Intraday Square Off)to CNC (Cash and Carry) and from CNC to MIS. You can find this option in your Position tab of Kite platform or mobile app.


1 Answers

The Rust language has a strong concept of ownership, which results in this "move vs. copy" scenario (I'm going to be using terminology from that answer in this one).

A shared reference &T is (normally) a read-only view of a T somewhere in memory. One important property is you're not allowed to invalidate that T: a &T must always point to a valid T instance.

When you write *x you're trying to move the T out by-value, since T is an unbounded generic the compiler has to assume the worst: the type T is not Copy and so must move ownership, that is, a by-value use (aka byte copy) is not a semantic copy, meaning the source cannot continue to be used (see my linked answer for more explanation of this). Moving ownership is invalidating the source... but the source is inside a &T, so invalidating it is illegal!

*x works for int because it is Copy, so a by-value use (aka byte copy) is the same as a semantic copy: the &int is not being invalidated.

like image 193
huon Avatar answered Oct 27 '22 19:10

huon