### Display and Debug Formatting for ColdString Source: https://context7.com/tomtomwombat/cold-string/llms.txt Covers the implementation of the Display and Debug traits for ColdString, enabling its use with `println!`, `format!`, and debug formatting specifiers. Examples show standard output, formatted strings, and debug representations. ```rust use cold_string::ColdString; let s = ColdString::new("hello world"); // Display formatting println!("{}", s); // Output: hello world let formatted = format!("Message: {}", s); assert_eq!(formatted, "Message: hello world"); // Debug formatting println!("{:?}", s); // Output: "hello world" let debug = format!("{:?}", s); assert_eq!(debug, "\"hello world\""); // Works with format specifiers let padded = format!("{:>15}", s); // Right-align in 15 chars assert_eq!(padded, " hello world"); ``` -------------------------------- ### Create ColdString from UTF-8 Bytes (Rust) Source: https://context7.com/tomtomwombat/cold-string/llms.txt Shows how to create a ColdString from a byte slice, ensuring UTF-8 validity. It includes examples for valid UTF-8 byte sequences, handling invalid sequences by returning a `Result`, and an unsafe method for performance when UTF-8 validity is guaranteed by the caller. ```rust use cold_string::ColdString; // Valid UTF-8 bytes (emoji: crab and 100) let bytes = [240, 159, 166, 128, 240, 159, 146, 175]; let compact = ColdString::from_utf8(&bytes).expect("valid UTF-8"); assert_eq!(compact, "\u{1F980}\u{1F4AF}"); // crab and 100 emoji // Invalid UTF-8 bytes let invalid_bytes = [255, 255, 255]; let result = ColdString::from_utf8(&invalid_bytes); assert!(result.is_err()); // Unsafe unchecked conversion (caller must ensure valid UTF-8) let sparkle_heart = [240, 159, 146, 150]; let heart = unsafe { ColdString::from_utf8_unchecked(&sparkle_heart) }; assert_eq!(heart.as_str(), "\u{1F496}"); // sparkle heart emoji ``` -------------------------------- ### Building ColdString from Iterators Source: https://context7.com/tomtomwombat/cold-string/llms.txt Shows how to construct a ColdString by collecting characters from an iterator using the FromIterator trait. Examples include collecting from a char array, filtering alphabetic characters, and transforming characters to uppercase. ```rust use cold_string::ColdString; // Collect from char iterator let chars = ['h', 'e', 'l', 'l', 'o']; let s: ColdString = chars.iter().copied().collect(); assert_eq!(s.as_str(), "hello"); // Filter and collect let mixed = "H3ll0 W0rld!"; let letters: ColdString = mixed.chars().filter(|c| c.is_alphabetic()).collect(); assert_eq!(letters.as_str(), "HllWrld"); // Transform and collect let text = "hello"; let upper: ColdString = text.chars().map(|c| c.to_ascii_uppercase()).collect(); assert_eq!(upper.as_str(), "HELLO"); ``` -------------------------------- ### ColdString Memory Layout and Struct Packing Efficiency (Rust) Source: https://context7.com/tomtomwombat/cold-string/llms.txt Explains the memory efficiency of ColdString by comparing its size and alignment to the standard `String`. ColdString occupies only 8 bytes with 1-byte alignment, enabling tight packing within structs without padding, as demonstrated with a `Record` struct example. ```rust use std::mem; use cold_string::ColdString; // ColdString is 8 bytes with 1-byte alignment assert_eq!(mem::size_of::(), 8); assert_eq!(mem::align_of::(), 1); // Packs efficiently with other types assert_eq!(mem::size_of::<(ColdString, u8)>(), 9); assert_eq!(mem::align_of::<(ColdString, u8)>(), 1); // Compare with standard String (24 bytes, 8-byte alignment) assert_eq!(mem::size_of::(), 24); assert_eq!(mem::align_of::(), 8); // Struct packing example struct Record { name: ColdString, age: u8, active: bool, } // Record is only 10 bytes due to 1-byte alignment assert_eq!(mem::size_of::(), 10); ``` -------------------------------- ### Check if ColdString Data is Stored Inline or on Heap (Rust) Source: https://context7.com/tomtomwombat/cold-string/llms.txt Provides examples of using the `is_inline()` method to determine if a ColdString's data is stored directly within its 8-byte structure (inline) or if it has been allocated on the heap. This is based on the string's byte length, with 8 bytes or less being stored inline. ```rust use cold_string::ColdString; // Strings up to 8 bytes are inlined let empty = ColdString::new(""); assert!(empty.is_inline()); let seven = ColdString::new("1234567"); // 7 bytes assert!(seven.is_inline()); let eight = ColdString::new("12345678"); // exactly 8 bytes assert!(eight.is_inline()); // Strings longer than 8 bytes use heap allocation let nine = ColdString::new("123456789"); // 9 bytes assert!(!nine.is_inline()); let long = ColdString::new("this is a much longer string"); assert!(!long.is_inline()); ``` -------------------------------- ### Basic ColdString Usage Source: https://github.com/tomtomwombat/cold-string/blob/main/README.md Demonstrates the basic usage of ColdString, showing how to create an instance and access its string slice representation. This is analogous to using a standard Rust String. ```rust use cold_string::ColdString; let s = ColdString::new("qwerty"); assert_eq!(s.as_str(), "qwerty"); ``` -------------------------------- ### Serde Serialization/Deserialization for ColdString Source: https://context7.com/tomtomwombat/cold-string/llms.txt Illustrates how to use ColdString with Serde for JSON serialization and deserialization when the 'serde' feature is enabled. It requires adding 'serde' to Cargo.toml features and demonstrates serializing/deserializing a struct containing ColdStrings. ```rust // In Cargo.toml: // [dependencies] // cold-string = { version = "0.1", features = ["serde"] } use cold_string::ColdString; use serde::{Serialize, Deserialize}; #[derive(Serialize, Deserialize)] struct User { name: ColdString, email: ColdString, } let user = User { name: ColdString::new("Alice"), email: ColdString::new("alice@example.com"), }; // Serialize to JSON let json = serde_json::to_string(&user).unwrap(); assert_eq!(json, r#"{"name":"Alice","email":"alice@example.com"}"#); // Deserialize from JSON let parsed: User = serde_json::from_str(&json).unwrap(); assert_eq!(parsed.name.as_str(), "Alice"); assert_eq!(parsed.email.as_str(), "alice@example.com"); ``` -------------------------------- ### ColdString Memory Packing Source: https://github.com/tomtomwombat/cold-string/blob/main/README.md Illustrates how ColdString packs efficiently with other data types, reducing memory overhead. It shows the size and alignment of ColdString and a tuple containing ColdString and a u8. ```rust use std::mem; use cold_string::ColdString; assert_eq!(mem::size_of::(), 8); assert_eq!(mem::align_of::(), 1); assert_eq!(mem::size_of::<(ColdString, u8)>(), 9); assert_eq!(mem::align_of::<(ColdString, u8)>(), 1); ``` -------------------------------- ### Equality Comparisons for ColdString Source: https://context7.com/tomtomwombat/cold-string/llms.txt Demonstrates how ColdString implements PartialEq for comparisons with other ColdStrings, &str, and str slices. It highlights fast byte-array comparison for inline strings and its compatibility with collections like HashSet. ```rust use cold_string::ColdString; let s1 = ColdString::new("hello"); let s2 = ColdString::new("hello"); let s3 = ColdString::new("world"); // Compare ColdStrings assert_eq!(s1, s2); assert_ne!(s1, s3); // Compare with &str assert_eq!(s1, "hello"); assert_eq!("hello", s1); // Compare with str slice let slice: &str = "hello"; assert_eq!(s1, slice); assert_eq!(slice, s1); // Works in collections use std::collections::HashSet; let mut set = HashSet::new(); set.insert(ColdString::new("apple")); set.insert(ColdString::new("banana")); assert!(set.contains("apple")); ``` -------------------------------- ### Hashing and Collection Usage for ColdString Source: https://context7.com/tomtomwombat/cold-string/llms.txt Explains how ColdString implements the Hash trait, enabling its use as keys in HashMap and HashSet. It also showcases the ergonomic lookups in collections due to the implementation of Borrow. ```rust use cold_string::ColdString; use std::collections::{HashMap, HashSet}; // Use in HashMap as key let mut map: HashMap = HashMap::new(); map.insert(ColdString::new("one"), 1); map.insert(ColdString::new("two"), 2); map.insert(ColdString::new("three"), 3); // Lookup with &str (thanks to Borrow) assert_eq!(map.get("one"), Some(&1)); assert_eq!(map.get("two"), Some(&2)); // Use in HashSet let mut set: HashSet = HashSet::new(); set.insert(ColdString::new("apple")); set.insert(ColdString::new("banana")); set.insert(ColdString::new("cherry")); // Check membership with &str assert!(set.contains("apple")); assert!(!set.contains("orange")); ``` -------------------------------- ### Cloning ColdStrings Source: https://context7.com/tomtomwombat/cold-string/llms.txt Details the Clone implementation for ColdString. It differentiates between cloning inline strings (fast, no allocation) and heap strings (allocates new memory), ensuring each clone is independent. ```rust use cold_string::ColdString; // Clone inline string (fast, no allocation) let short = ColdString::new("short"); let short_clone = short.clone(); assert_eq!(short, short_clone); assert!(short.is_inline()); // Clone heap string (allocates new memory) let long = ColdString::new("this is a longer string on the heap"); let long_clone = long.clone(); assert_eq!(long, long_clone); assert!(!long.is_inline()); // Each clone is independent drop(long); assert_eq!(long_clone.as_str(), "this is a longer string on the heap"); ``` -------------------------------- ### Access ColdString Content as Slices and Use String Methods (Rust) Source: https://context7.com/tomtomwombat/cold-string/llms.txt Illustrates how to access the string data within a ColdString as a `&str` or `&[u8]` slice using `as_str()` and `as_bytes()`. It also demonstrates leveraging the `Deref` trait implementation to directly call standard `str` methods like `starts_with`, `contains`, `to_uppercase`, and character iteration. ```rust use cold_string::ColdString; let s = ColdString::new("hello"); // Get as string slice assert_eq!(s.as_str(), "hello"); // Get as byte slice assert_eq!(s.as_bytes(), &[104, 101, 108, 108, 111]); // Get length in bytes assert_eq!(s.len(), 5); // Use Deref to access str methods directly assert!(s.starts_with("hel")); assert!(s.contains("ell")); let upper: String = s.to_uppercase(); assert_eq!(upper, "HELLO"); // Iterate over characters for c in s.chars() { println!("{}", c); } ``` -------------------------------- ### Create ColdString from String References and Literals (Rust) Source: https://context7.com/tomtomwombat/cold-string/llms.txt Demonstrates creating a ColdString from various string types like string literals and owned `String` objects. It highlights how short strings (<= 8 bytes) are stored inline, while longer ones are heap-allocated, using the `ColdString::new` constructor and the `From` trait. ```rust use cold_string::ColdString; // Create from string literal let s = ColdString::new("hello"); assert_eq!(s.as_str(), "hello"); // Create from String let owned = String::from("world"); let s2 = ColdString::new(&owned); assert_eq!(s2.as_str(), "world"); // Create using From trait let s3 = ColdString::from("qwerty"); let s4: ColdString = "inline".into(); // Short strings are inlined (8 bytes or less) let short = ColdString::new("short"); assert!(short.is_inline()); // Longer strings are heap-allocated let long = ColdString::new("this is a longer string"); assert!(!long.is_inline()); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.