Categories
Tags
3d algorithms alignment analyze APIT Arc Architecture arm ascii assembly asynchronous base64 BitHacks Blogging box c c23 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 doc DP drawio dtruss Dynamic emulator example Example FFI 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 interop invariant iterator join justfile kernel LaTeX leak LFU linux lto MachineLearning macOS map Markdown math ML mmap mod nc OnceLock optimization OS ownership panic parallels perf physics pin postgresql product profiling pub radare2 rayon release reverse RPIT rust sanitizer Science science serialization server shift sin size SmallProjects socket std strace String StringView strip strlen struct sum super surrealdb SWAR swisstable synchronous tan thread time toml tracing traits triangulation uint32_t UnsafeRust utf16 utf8 Video vulkan wsl x86_64 xilem zig
106 words
1 minutes
250103_string_append
link
Rust Code
#[derive(Debug)]
struct Young {
data: String,
}
impl Young {
fn new() -> Self {
Self {
data: "".to_string(),
}
}
// append new text after original
fn input(&mut self, x: &str) {
self.data.push_str(x);
}
}
fn main() {
let my_da = Young {
data: "young".to_string(),
};
let my_da02 = Young {
data: "young".to_string(),
};
let data = Young::new();
let mut data2 = my_da02;
println!("Hello, world! {data:?}");
println!("Hello, world! {data2:?}");
data2.input("testtest");
println!("After input: {data2:?}");
}Output
Hello, world! Young { data: "" }
Hello, world! Young { data: "young" }
After input: Young { data: "youngtesttest" }✅ Why push_str is correct
- In Rust
| method | meaning |
|---|---|
= | replace |
+ | move + concat |
push_str | append safely |
format! | create new string |
250103_string_append
https://younghakim7.github.io/blog/posts/250103_string_append/