Categories
Tags
3d ABI Actions algorithms alignment analyze APIT Arc Architecture arm array ascii ash assembly asynchronous attribute base64 bithacks Blogging box c c23 cdylib CHAR_BIT clamp clang clang-format client clippy cmake compiler Computer concat concurrency const_fn constexpr contravariant cos covariant cpp cpu crate CS Customization cybersecurity DataStructure db debugging Demo deserialization discrete dll doc DP drawio dtruss dylib Dynamic emulator example Example FFI ffmpeg flamegraph flat_map fold format FP fsanitize Functional FunctionalProgramming functions futures Fuwari game GATs gcc gccrs generics gitignore glibc GUI hacking hashmap haskell heap hyperfine Imperative inference intel interop invariant iterator join justfile kernel LaTeX leak LFU limits linux lto MachineLearning macOS map Markdown math ML mmap mod mp3 nc nextest OnceLock optimization OS ownership panic parallels perf physics pin postgresql procedure product profiling pub radare2 rayon release reverse RPIT rust sanitizer Science science serialization server shift SIMD sin size SmallProjects so socket std strace String StringView strip strlen struct sum super surrealdb SWAR swisstable synchronous syscall tan test thread time toml tracing traits trap triangulation type uint32_t UnsafeRust utf16 utf8 vector Video vulkan wsl x86_64 xilem zig
221 words
1 minutes
250104_string_impl_concat_default
link
Rust Code
#[derive(Debug)]
struct Young {
data: String,
}
impl Default for Young {
fn default() -> Self {
Self {
data: "my_default".to_string(),
}
}
}
impl Young {
fn new() -> Self {
Self {
data: String::new(),
}
}
// append string slice
fn input(&mut self, x: &str) {
self.data.push_str(x);
}
// push a single character
fn push_char(&mut self, c: char) {
self.data.push(c);
}
// concat using format!
fn concat(&mut self, x: &str) {
self.data = format!("{}{}", self.data, x);
}
// join multiple strings
fn join_with(&mut self, parts: &[&str], sep: &str) {
self.data = parts.join(sep);
}
}
fn main() {
let data = Young::new();
let mut data2 = Young {
data: "young".to_string(),
};
println!("Initial data: {data:?}");
println!("Initial data2: {data2:?}");
// push_str
data2.input(" testtest");
println!("After push_str: {data2:?}");
// push (single char)
data2.push_char('!');
println!("After push: {data2:?}");
// concat
data2.concat(" CONCAT");
println!("After concat: {data2:?}");
// join
let parts = ["Rust", "is", "fast"];
data2.join_with(&parts, " ");
println!("After join: {data2:?}");
// default
let default_test = Young::default();
println!("{default_test:?}");
}- result
Initial data: Young { data: "" }
Initial data2: Young { data: "young" }
After push_str: Young { data: "young testtest" }
After push: Young { data: "young testtest!" }
After concat: Young { data: "young testtest! CONCAT" }
After join: Young { data: "Rust is fast" }
Young { data: "my_default" }250104_string_impl_concat_default
https://younghakim7.github.io/blog/posts/250104_string_impl_concat_default/