std::char::from_digit

Function std::char::from_digit

pub fn from_digit(num: u32, radix: u32) -> Option<char>

Converts a digit in the given radix to a char.

A 'radix' here is sometimes also called a 'base'. A radix of two indicates a binary number, a radix of ten, decimal, and a radix of sixteen, hexadecimal, to give some common values. Arbitrary radices are supported.

from_digit() will return None if the input is not a digit in the given radix.

Panics

Panics if given a radix larger than 36.

Examples

Basic usage:

use std::char;

let c = char::from_digit(4, 10);

assert_eq!(Some('4'), c);

// Decimal 11 is a single digit in base 16
let c = char::from_digit(11, 16);

assert_eq!(Some('b'), c);

Returning None when the input is not a digit:

use std::char;

let c = char::from_digit(20, 10);

assert_eq!(None, c);

Passing a large radix, causing a panic:

use std::thread;
use std::char;

let result = thread::spawn(|| {
    // this panics
    let c = char::from_digit(1, 37);
}).join();

assert!(result.is_err());

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/char/fn.from_digit.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部