Is it possible to specialize on a static lifetime?

后端 未结 2 1378
我寻月下人不归
我寻月下人不归 2021-01-19 10:49

I want to specialize &\'static str from &\'a str. Something like this:

use std::borrow::Cow;

struct MyString {
    inner:          


        
2条回答
  •  灰色年华
    2021-01-19 11:46

    I write this answer after reading this duplicated post asking how to define a method/function that behaves differently when it is passed a static string or a non-static string.

    This is not possible, so a workaround may be using a wrapper type to wrap the string argument in an enum:

    enum MyString {
        Static(&'static str),
        Heap(String),
    }
    
    fn bar(arg: &MyString) {
        match arg {
            &MyString::Static(ref name) => println!("my first pc was {}", name),
            &MyString::Heap(ref name) => println!("I dont know {}", name),
        }
    }
    
    fn main() {
        let mut v = Vec::new();
    
        let forever: &'static str = "zx-spectrum";
        let local: &str = &"commodore64".to_string();
    
        v.push(MyString::Static(forever));
    
        // ERROR: try to insert 'a lifetime
        // v.push(Mystring::Static(local));
        v.push(MyString::Heap(local.to_string()));
    
        v.push(MyString::Heap("muggle".to_string()));
    
        bar(&v[0]);
        bar(&v[1]);
    }
    

    MyString stores a statically-allocated string literal as a &'static str and all other strings as a String.

    As pointed in the comments below, the standard library provides a type that fits the borrowed/owned case: the smart pointer Cow.

    The enum MyString used in this example is just a specific enum for managing string types.

    The only difference stems from a somewhat more specific naming of the enum and its variants related to the specific usage: MyString::Static("forever") versus Cow::Borrowed("forever") and MyString::Heap(str) versus Cow::Owned(str).

    Does this help improve mnemonics and code readability? I'm quite sure that this holds only for novices or occasional Rust programmers, not for seasoned Rustaceans.

提交回复
热议问题