Crates.io | rustring_builder |
lib.rs | rustring_builder |
version | 0.2.0 |
source | src |
created_at | 2023-06-19 18:59:06.865788 |
updated_at | 2023-06-20 00:32:40.277845 |
description | A Rust string builder |
homepage | |
repository | |
max_upload_size | |
id | 894320 |
size | 22,971 |
Basic usage:
use rustring_builder::StringBuilder;
fn add_strings_example()
{
let mut sb = StringBuilder::new();
sb.append("Hello");
sb.append(" ");
sb.append("World");
sb.append("!");
let s = sb.to_string();
assert_eq!(s, "Hello World!");
}
use rustring_builder::StringBuilder;
fn add_types_example()
{
let mut sb = StringBuilder::new();
sb.append(1);
sb.append(" ");
sb.append(2.0);
sb.append(" ");
sb.append("three");
sb.append(" ");
sb.append(true);
let s = sb.to_string();
assert_eq!(s, "1 2 three true");
}
use rustring_builder::StringBuilder;
fn append_string_builder_example()
{
let mut sb = StringBuilder::new();
sb.append("Hello");
sb.append(" ");
sb.append("World");
sb.append("!");
let s = sb.to_string();
assert_eq!(s, "Hello World!");
sb.append(" Goodbye!");
let s = sb.to_string();
assert_eq!(s, "Hello World! Goodbye!");
}
use rustring_builder::StringBuilder;
fn add_another_string_builder_example()
{
let mut sb = StringBuilder::new();
sb.append("Hello");
sb.append(" ");
sb.append("World");
sb.append("!");
let s = sb.to_string();
assert_eq!(s, "Hello World!");
let mut sb2 = StringBuilder::new();
sb2.append(" Goodbye!");
sb += sb2;
let s = sb.to_string();
assert_eq!(s, "Hello World! Goodbye!");
}
in this example, sb2 is consumed by the += operator, and can no longer be used.
use rustring_builder::StringBuilder;
fn push_example()
{
let mut sb = StringBuilder::new();
sb.append("Hello");
sb.append(" ");
sb.append("World");
sb.append("!");
let s = sb.to_string();
assert_eq!(s, "Hello World!");
sb.push(' ');
sb.push('G');
sb.push('o');
sb.push('o');
sb.push('d');
sb.push('b');
sb.push('y');
sb.push('e');
sb.push('!');
let s = sb.to_string();
assert_eq!(s, "Hello World! Goodbye!");
}
This method might be useful if you are building a string from a stream of characters, or if you are building a string from a file.
use rustring_builder::StringBuilder;
fn nth_example()
{
let mut sb = StringBuilder::new();
sb.append("Hello");
sb.append(" ");
sb.append("World");
sb.append("!");
let s = sb.to_string();
assert_eq!(s, "Hello World!");
assert_eq!(sb.nth(0), Some('H'));
assert_eq!(sb.nth(1), Some('e'));
assert_eq!(sb.nth(2), Some('l'));
assert_eq!(sb.nth(3), Some('l'));
assert_eq!(sb.nth(4), Some('o'));
assert_eq!(sb.nth(5), Some(' '));
assert_eq!(sb.nth(6), Some('W'));
assert_eq!(sb.nth(7), Some('o'));
assert_eq!(sb.nth(8), Some('r'));
assert_eq!(sb.nth(9), Some('l'));
assert_eq!(sb.nth(10), Some('d'));
assert_eq!(sb.nth(11), Some('!'));
assert_eq!(sb.nth(12), None);
}