Rust

Rust

By DeepSource

Found occurrence of `Box::new("..")`RS-W1058

Anti-pattern

Prefer Box::from("..") over Box::new("..").Box::new("..")simply stores a _pointer_ to those bytes, on the heap, whereas,Box::from("..")actually allocates bytes of thestr` on the heap.

Box<&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 Box::from(<str>) or String::from(<str>).

Examples

Bad practice

fn foo() -> Box<&str> {
    Box::new("a str")
}

Recommended

fn foo() -> Box<str> {
    Box::from("a str")
}