How to get the byte offset between `&str`

后端 未结 2 1098
甜味超标
甜味超标 2020-12-07 02:37

I have two &str pointing to the same string, and I need to know the byte offset between them:

fn main() {
    let foo = \"  bar\";
         


        
2条回答
  •  醉酒成梦
    2020-12-07 03:08

    This is of course kind of unsafe, but if you want arithmetic, you can just cast the pointers to usize with as and subtract that.

    (Note: it's not so unsafe that the compiler will actually complain.)

提交回复
热议问题