Quantcast
Channel: Create owned value on demand - Stack Overflow
Browsing all 3 articles
Browse latest View live

Answer by fred xia for Create owned value on demand

The code is somewhat equivalent to the C++ code below:#include <string>int main(){ auto condition = false; std::string a("a"); const std::string* ptr; if (condition) { ptr = &a; } else {...

View Article



Answer by PitaJ for Create owned value on demand

Here's how to use Cow in your case:use std::borrow::Cow;let condition = false;let a: String = "a".to_string();let r: Cow<str> = if condition { Cow::from(&a)} else { let b: String =...

View Article

Create owned value on demand

There is an existing value a, and I want to get a reference to either a or a new value b created on demand, depending on some condition. The code below won't compile. I would like to know what is the...

View Article
Browsing all 3 articles
Browse latest View live




Latest Images