I want the members to be owned by the struct. Sorry for the simple question, but I wasn't able to find an example. I'm looking for the correct declaration of a struct and instantiation examples.
相关问题
- how to split a list into a given number of sub-lis
- Generate string from integer with arbitrary base i
- Share Arc between closures
- Converting a string array to a byte array
- Function references: expected bound lifetime param
相关文章
- How can I convert a f64 to f32 and get the closest
- JSP String formatting Truncate
- What is a good way of cleaning up after a unit tes
- Selecting only the first few characters in a strin
- Python: print in two columns
- extending c++ string member functions
- Google app engine datastore string encoding proble
- How can I unpack (destructure) elements from a vec
If the string has to be owned by the struct, then you should use
String
. Alternatively, you could use an&str
with a static lifetime (i.e., the lifetime of the program). For example:If the lifetime of the string is unknown, then you can parameterize
Foo
with a lifetime:See also:
If you're not sure whether the string will be owned or not (useful for avoiding allocations), then you can use
borrow::Cow
:Note that the
Cow
type is parameterized over a lifetime. The lifetime refers to the lifetime of the borrowed string (i.e., when it is aBorrowed
). If you have aCow
, then you can useborrow
and get a&'a str
, with which you can do normal string operations without worrying about whether to allocate a new string or not. Typically, explicit calling ofborrow
isn't required because of deref coercions. Namely,Cow
values will dereference to their borrowed form automatically, so&*val
whereval
has typeCow<'a, str>
will produce a&str
.