to Int() - > Option<int>- 모듈
- 문자열 →
- 수신자
- string
- 최소 인자 수
- 0
- 최대 인자 수
- 0
Declaration and parameters
사용 조건
기본 포함 · 예
These types are provided by the language. No module import is required.
The declarations below describe the standard operations on built-in values.
수신자
string
매개변수 제약
string. returns Option<int>. It accepts an optional leading - followed by one or more ASCII digits 0 through 9, and no other characters.
Leading zeros are accepted. - produces 0.
Leading or trailing whitespace, a leading +, digit separators, radix prefixes, exponents, fractions and non-ASCII digits return None(). Empty text and a lone - also return None().
A spelled value below -9223372036854775808 or above 9223372036854775807 returns None(). The call never faults for its text.
Call trim() before to when the input format allows surrounding whitespace.
결과
The declared result type is Option<int>.
효과
실패
Result and effects
Return None() for any other spelling and for a value outside the int range.
Related entries
Parse decimal integers with one spelling
Parse decimal integers with one spelling
string. returns Option<int>. It accepts an optional leading - followed by one or more ASCII digits 0 through 9, and no other characters.
Leading zeros are accepted. - produces 0.
Leading or trailing whitespace, a leading +, digit separators, radix prefixes, exponents, fractions and non-ASCII digits return None(). Empty text and a lone - also return None().
A spelled value below -9223372036854775808 or above 9223372036854775807 returns None(). The call never faults for its text.
Call trim() before to when the input format allows surrounding whitespace.
이유
One number has one accepted spelling, as in BigInt and Decimal text parsing. Rejecting whitespace and a plus sign keeps malformed input visible to the program, so a reader of wire formats does not accept a value that its writer never produces.
Parse a minus sign and decimal digits
import std.iofunction show(text: string) -> string {match (text.toInt()) {case Some(value) => "{value}"case None() => "none"}}for text in (["42", "-7", "007", "-0", "9223372036854775807", "-9223372036854775808"]) {io.writeLine("[{text}] {show(text)}")}
[42] 42
[-7] -7
[007] 7
[-0] 0
[9223372036854775807] 9223372036854775807
[-9223372036854775808] -9223372036854775808
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
[42] 42[-7] -7[007] 7[-0] 0[9223372036854775807] 9223372036854775807[-9223372036854775808] -9223372036854775808
Return None() for other spellings and out-of-range values
import std.iofunction show(text: string) -> string {match (text.toInt()) {case Some(value) => "{value}"case None() => "none"}}for text in (["", "-", "12a", "1_000", "0x10", "1e3", "١٢", "9223372036854775808", "-9223372036854775809"]) {io.writeLine("[{text}] {show(text)}")}
[] none
[-] none
[12a] none
[1_000] none
[0x10] none
[1e3] none
[١٢] none
[9223372036854775808] none
[-9223372036854775809] none
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
[] none[-] none[12a] none[1_000] none[0x10] none[1e3] none[١٢] none[9223372036854775808] none[-9223372036854775809] none
Return None() for surrounding whitespace and a plus sign
import std.iofunction show(text: string) -> string {match (text.toInt()) {case Some(value) => "{value}"case None() => "none"}}for text in ([" 12", "12 ", "+5"]) {io.writeLine("[{text}] {show(text)}")}
[ 12] none
[12 ] none
[+5] none
기대 결과
기대 결과
실행을 마칩니다. 종료 코드 0.
[ 12] none[12 ] none[+5] none