slice(start: int, end: int) - > Bytes- 수신자
- Bytes
- 최소 인자 수
- 2
- 최대 인자 수
- 2
Declaration and parameters
링크사용 조건
기본 포함 · 미정
Import std.bytes in language mode 5.21.
The current compiler selects std/bytes.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.
수신자
Bytes
매개변수 제약
Bytes.slice uses a start position and an exclusive end position measured in bytes.
Valid bounds satisfy 0 <= start <= end <= length. The result contains exactly the bytes from start through end minus one.
Equal endpoints produce empty Bytes, including endpoints at zero and at length. A full interval preserves every byte.
Negative bounds, inverted bounds, and endpoints beyond length produce TPZ4001 at runtime. Bounds are not clamped or interpreted from the end.
Slicing never changes the receiver. The receiver, start, and end expressions evaluate once in that order before the bounds are checked.
A byte interval may split a UTF-8 encoding. Bytes.slice preserves those selected octets and Bytes.decodeUtf8 reports invalid text when they form an incomplete sequence.
관련 규칙 · Select explicit byte intervals →결과
The declared result type is Bytes.
효과
실패
Result and effects
링크Copy the half-open interval from start to end. Invalid or inverted bounds produce TPZ4001 at runtime.
Resolved contracts
링크Related entries
링크Select explicit byte intervals
링크Select explicit byte intervals
Bytes.slice uses a start position and an exclusive end position measured in bytes.
Valid bounds satisfy 0 <= start <= end <= length. The result contains exactly the bytes from start through end minus one.
Equal endpoints produce empty Bytes, including endpoints at zero and at length. A full interval preserves every byte.
Negative bounds, inverted bounds, and endpoints beyond length produce TPZ4001 at runtime. Bounds are not clamped or interpreted from the end.
Slicing never changes the receiver. The receiver, start, and end expressions evaluate once in that order before the bounds are checked.
A byte interval may split a UTF-8 encoding. Bytes.slice preserves those selected octets and Bytes.decodeUtf8 reports invalid text when they form an incomplete sequence.
이유
An explicit interval should identify exactly the requested payload. Rejecting an invalid interval makes a mistaken byte count visible before subsequent decoding.
Select empty and nonempty intervals
링크import std.iolet value = "abcd".encodeUtf8()io.writeLine(value.slice(0, 0).toHex())io.writeLine(value.slice(1, 3).toHex())io.writeLine(value.slice(4, 4).toHex())io.writeLine(value.toHex())
6263
61626364
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
626361626364
Keep byte slicing separate from UTF-8 validation
링크import std.iolet byte = "é".encodeUtf8().slice(0, 1)io.writeLine(byte.toHex())match (byte.decodeUtf8()) {case Err(_) => io.writeLine("invalid UTF-8")case Ok(_) => io.writeLine("accepted")}
c3
invalid UTF-8
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
c3invalid UTF-8
Reject a negative slice start
링크import std.bytes { Bytes }let value = "abcd".encodeUtf8()value.slice(-1, 2)
표준 출력 없음.기대 결과
Reject an inverted byte interval
링크import std.bytes { Bytes }let value = "abcd".encodeUtf8()value.slice(3, 2)
표준 출력 없음.기대 결과
Reject an endpoint beyond the sequence
링크import std.bytes { Bytes }let value = "abcd".encodeUtf8()value.slice(0, 5)
표준 출력 없음.