A primitive type is one of Rust’s simplest types. Integers and characters are two examples. Even these basic types involve choices: an integer’s type determines whether it can be negative and how large it can grow. section 1.3
Rust’s integer types represent whole numbers, with no decimal point:
- Signed types can hold negative numbers, zero, or positive numbers. Their names begin with i: i8, i16, i32, i64, i128, and isize. section 1.3
- Unsigned types hold only nonnegative numbers. Their names begin with u: u8, u16, u32, u64, u128, and usize. section 1.3
- In names such as u8 and u16, the number gives the number of bits. More bits allow larger values: u8 holds up to 255, while u16 holds up to 65,535. section 1.3
A bit is a place that holds either 0 or 1. Eight bits make one byte. Thinking about those bits helps explain why Rust offers several types for the same everyday idea of a whole number. section 1.3
A character and a type correction
A character can be a letter, a space, or a symbol: `let first_letter = 'A';` and `let cat_face = '😺';` are both valid examples. Type matters when converting a number to a character. If `let my_number = 100;` is followed by `my_number as char`, the compiler reports that it cannot cast the inferred `i32` directly to `char`; the conversion requires `u8`. Writing `let my_number: u8 = 100;` makes the intended type explicit. section 1.3
fn main() {
let str1 = "Hello!";
println!("str1 is {} bytes and also {} characters.", str1.len(), str1.chars().count());
let str2 = "안녕!";
println!("str2 is {} bytes but only {} characters.", str2.len(), str2.chars().count());
}
Compare `.len()` with `.chars().count()` in this runnable example. `"안녕!"` occupies seven bytes but contains three characters, so a string’s byte length is not always its character count. section 1.3





