Https://doc.rust-lang.org/std/mem/fn.transmute.html)的官方文档包括一个有福的代码示例,其中包含以下注释
// This now has three mutable references pointing at the same
// memory. `slice`, the rvalue ret.0, and the rvalue ret.1.
// `slice` is never used after `let ptr = ...`, and so one can
// treat it as "dead", and therefore, you only have two real
// mutable slices.
这似乎是正式文档的手动波动(当我可以将可变的裁判视为“死”',我什么时候可以声称某些可变的裁判是“真实的”,有些不是),尤其是因为呼唤可能看起来像这样,我可以合理地声称在指定的点之后使用了slice
fn bar<T>(slice: &mut [T]) { ... }
fn foo<T>(slice: &mut [T]) {
{
let (front, back) = split_at_stdlib(slice, 5);
bar(front);
bar(back);
}
bar(slice);
}
slice
split_at_stdlib
参数,我们可以清楚地看到这一点,因为
slice
返回而无需再次使用。caller's
输入切片无关。 这是因为Rust的正常寿命规则将阻止输入切片被使用,直到返回的子链接死亡为止。 如果我们在签名中省去了寿命,那么我们就会得到:
fn split_at_stdlib<'a, T>(slice: &'a mut [T], mid: usize) -> (&'a mut [T], &'a mut [T])
这意味着返回的切片是从输入切片借来的。 Rust根本不会让您再次使用slice