我对此类型&str
的错误消息感到困惑:
let a = &String::from("abcdefg"); // ok!
let a = String::from("abcdefg").as_str(); // compile error
错误:
let a = String::from("abcdefg").as_str();
^^^^^^^^^^^^^^^^^^^^^^^ - temporary value dropped here while still borrowed
temporary value does not live long enough
据我所知,在第二行中,String
对象是一个临时对象,当行结束时它会丢弃。但为什么第一行运行正常?