Skip to content

Determine string length at compile time #2039

Closed
@jD91mZM2

Description

@jD91mZM2

So, imagine having a len! macro. Literally just that.

Why is this useful?

Well, there aren't a lot of reasons...
Use-case #1:

let world = "World";
let helloworld = String::with_capacity(len!("Hello "), world.len());
println!("{}", helloworld);

Use-case #2:

let string = "prefix!!yay";
if string.starts_with("prefix!!") {
    let string = &string[len!("prefix!!")..];
    assert_eq!(string, "yay");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiRelevant to the library API team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions