Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::rc::Rc;
- enum StringOrSlice<'a> {
- Owned(String),
- Borrowed(&'a str),
- }
- struct MyString<'a> {
- data: StringOrSlice<'a>,
- }
- impl<'a> MyString<'a> {
- fn new_owned(data: String) -> Self {
- MyString {
- data: StringOrSlice::Owned(data),
- }
- }
- fn new_borrowed(data: &'a str) -> Self {
- MyString {
- data: StringOrSlice::Borrowed(data),
- }
- }
- fn as_ref(&self) -> &str {
- match &self.data {
- StringOrSlice::Owned(s) => &s,
- StringOrSlice::Borrowed(s) => s,
- }
- }
- fn clone(&self) -> Self {
- match &self.data {
- StringOrSlice::Owned(s) => MyString::new_owned(s.clone()),
- StringOrSlice::Borrowed(s) => MyString::new_borrowed(s),
- }
- }
- fn to_owned(&self) -> MyString<'static> {
- match &self.data {
- StringOrSlice::Owned(s) => MyString::new_owned(s.clone()),
- StringOrSlice::Borrowed(s) => MyString::new_owned(s.to_owned()),
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement