Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion from Option<&str> to Option<String>

Tags:

rust

What is the best practice to convert Option<&str> to Option<String>? Strictly speaking I'm looking for a concise equivalent of:

if s.is_some() {
    Some(s.to_string())
} else {
    None
}

and this is the best I could come up with:

s.and_then(|s| Some(s.to_string()))
like image 750
mariusz Avatar asked Nov 30 '14 23:11

mariusz


People also ask

What is conversion option in a loan?

A conversion loan is a loan that rolls over, or converts, to a different loan structure after a certain term. Pricing both pieces of the loan at once allows you to account for the sequential closing and funding dates in the opportunity profitability calculations.

Can I convert option to equity?

The Lender shall have the option, exercisable in Lender's sole and absolute discretion, to convert the amounts due under the Note into equity of the Borrower in accordance with the Plan of Reorganization of the Borrower in form and substance agreeable to the Lender in its sole discretion.

What is conversion and reversion?

In simple words, a Conversion is when stock is being bought in order to synthetically close out a short stock position that is created synthetically by long put and short call of the same strike. Reversal is when stock is being shorted/ sold in order to synthetically close out a synthetic long stock position.

How do you convert options to shares?

When you convert a call option into stock by exercising, you now own the shares. You must use cash that will no longer be earning interest to fund the transaction, or borrow cash from your broker and pay interest on the margin loan. In both cases, you are losing money with no offsetting gain.


2 Answers

map is a better choice:

s.map(|s| s.to_string())

or

s.map(str::to_string)
like image 139
Francis Gagné Avatar answered Sep 21 '22 15:09

Francis Gagné


Another way is to use s.map(str::to_string):

let reference: Option<&str> = Some("whatever");
let owned: Option<String> = reference.map(str::to_string);

I personally find it cleaner without the extra closure.

like image 41
Nikon the Third Avatar answered Sep 20 '22 15:09

Nikon the Third