Skip to content

Commit 006e8bf

Browse files
authored
Translate vec-insert-remove.md (#58)
1 parent 0b8318e commit 006e8bf

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
* [プッシュとポップ](vec-push-pop.md)
4747
* [デアロケーティング](vec-dealloc.md)
4848
* [参照外し](vec-deref.md)
49-
* [Insert and Remove](vec-insert-remove.md)
49+
* [挿入と削除](vec-insert-remove.md)
5050
* [IntoIter](vec-into-iter.md)
5151
* [RawVec](vec-raw.md)
5252
* [Drain](vec-drain.md)

src/vec-insert-remove.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,51 @@
1+
<!--
12
# Insert and Remove
3+
-->
24

5+
# 挿入と削除
6+
7+
<!--
38
Something *not* provided by slice is `insert` and `remove`, so let's do those
49
next.
10+
-->
11+
12+
スライスから提供*されない*ものに、 `insert``remove` があります。
13+
今度はこれらを実装していきましょう。
514

15+
<!--
616
Insert needs to shift all the elements at the target index to the right by one.
717
To do this we need to use `ptr::copy`, which is our version of C's `memmove`.
818
This copies some chunk of memory from one location to another, correctly
919
handling the case where the source and destination overlap (which will
1020
definitely happen here).
21+
-->
1122

23+
挿入では、挿入位置から最後の要素まで、 1 ずつずらす必要があります。
24+
これを行なうために、 `ptr::copy` を使う必要があります。 C の `memmove` の、
25+
Rust 版のようなものです。これは、ある場所のメモリを別の場所にコピーします。
26+
そして、2つの場所が重なっていても、正しくコピーされます (今回の場合、
27+
明らかに重なります) 。
28+
29+
<!--
1230
If we insert at index `i`, we want to shift the `[i .. len]` to `[i+1 .. len+1]`
1331
using the old len.
32+
-->
33+
34+
インデックス `i` の位置に挿入する場合、古い `len` の値を用いて、
35+
`[i .. len]``[i+1 .. len+1]` にシフトします。
1436

1537
```rust,ignore
1638
pub fn insert(&mut self, index: usize, elem: T) {
17-
// Note: `<=` because it's valid to insert after everything
18-
// which would be equivalent to push.
39+
// 注意: 全要素の後に挿入しても問題ないため、
40+
// `<=` としています。これは、プッシュと同等です。
41+
42+
// 境界外インデックスです
1943
assert!(index <= self.len, "index out of bounds");
2044
if self.cap == self.len { self.grow(); }
2145
2246
unsafe {
2347
if index < self.len {
24-
// ptr::copy(src, dest, len): "copy from source to dest len elems"
48+
// ptr::copy(src, dest, len): "src から dest まで len 個の要素をコピー"
2549
ptr::copy(self.ptr.offset(index as isize),
2650
self.ptr.offset(index as isize + 1),
2751
self.len - index);
@@ -32,12 +56,19 @@ pub fn insert(&mut self, index: usize, elem: T) {
3256
}
3357
```
3458

59+
<!--
3560
Remove behaves in the opposite manner. We need to shift all the elements from
3661
`[i+1 .. len + 1]` to `[i .. len]` using the *new* len.
62+
-->
63+
64+
削除では真逆の事を行ないます。*新しい* len を使用して、
65+
`[i+1 .. len+1]``[i .. len]` にシフトします。
3766

3867
```rust,ignore
3968
pub fn remove(&mut self, index: usize) -> T {
40-
// Note: `<` because it's *not* valid to remove after everything
69+
// 注意: 全要素のあとの物を削除することは*有効ではない*ため、 '<' を使用します
70+
71+
// 境界外インデックスです
4172
assert!(index < self.len, "index out of bounds");
4273
unsafe {
4374
self.len -= 1;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy