Return local String as a slice (&str)

后端 未结 5 1552
温柔的废话
温柔的废话 2020-11-21 06:27

There are several questions that seem to be about the same problem I\'m having. For example see here and here. Basically I\'m trying to build a String in a loca

5条回答
  •  有刺的猬
    2020-11-21 07:10

    Yes you can - the method replace_range provides a work around -

    let a = "0123456789";
    //println!("{}",a[3..5]);  fails - doesn't have a size known at compile-time
    let mut b = String::from(a);
    b.replace_range(5..,"");
    b.replace_range(0..2,"");
    println!("{}",b); //succeeds 
    

    It took blood sweat and tears to achieve this!

提交回复
热议问题