Rust

Rust

By DeepSource

Found occurrence of Rc::new("..") RS-W1062

Anti-pattern

Prefer Rc::from("..") over Rc::new("..").Rc<&str>` is a pointer to a pointer. This adds another level of indirection without any benefit whatsoever.

If the intention is to place a str on the heap, consider using Rc::from(<str>).

In summary:

  • Rc::new("..") simply stores a pointer to bytes (&str), on the heap
  • Rc::from("..") actually allocates bytes of the str on the heap

Examples

Bad practice

fn get_rc_str() -> Rc<&'static str> {
    Rc::new("a str")
}

Recommended

fn get_rc_str() -> Rc<str> {
    Rc::from("a str") // Is better
}