From 272a88687d87f86660b1c8bab8733ab0e14d111c Mon Sep 17 00:00:00 2001 From: Rustln Date: Thu, 14 Apr 2022 17:57:03 +0800 Subject: [PATCH] Fixed code typo. --- src/advance/unsafe/superpowers.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/advance/unsafe/superpowers.md b/src/advance/unsafe/superpowers.md index e76f4659..b56b905e 100644 --- a/src/advance/unsafe/superpowers.md +++ b/src/advance/unsafe/superpowers.md @@ -177,7 +177,7 @@ fn main() { let r = &mut v[..]; - let (a, b) = r.split_at_mut(3); + let (a, b) = split_at_mut(r, 3); assert_eq!(a, &mut [1, 2, 3]); assert_eq!(b, &mut [4, 5, 6]); @@ -219,6 +219,17 @@ fn split_at_mut(slice: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) { ) } } + +fn main() { + let mut v = vec![1, 2, 3, 4, 5, 6]; + + let r = &mut v[..]; + + let (a, b) = split_at_mut(r, 3); + + assert_eq!(a, &mut [1, 2, 3]); + assert_eq!(b, &mut [4, 5, 6]); +} ``` 相比安全实现,这段代码就显得没那么好理解了,甚至于我们还需要像 C 语言那样,通过指针地址的偏移去控制数组的分割。