Rfcs: Determine string length at compile time

Created on 23 Jun 2017  路  4Comments  路  Source: rust-lang/rfcs

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");
}
T-libs

Most helpful comment

Except "prefix!!".len() isn't retrieved at compile time.

All 4 comments

Both of those examples could use .len().

Except "prefix!!".len() isn't retrieved at compile time.

@legolord208 What difference does it make? It still optimizes to a constant, always.

Oh :thinking:.... Ooops, didn't realize

Was this page helpful?
0 / 5 - 0 ratings

Related issues

3442853561 picture 3442853561  路  4Comments

yongqli picture yongqli  路  3Comments

steveklabnik picture steveklabnik  路  4Comments

mqudsi picture mqudsi  路  3Comments

3442853561 picture 3442853561  路  3Comments