Categories
Tags
algorithms APIT arm assembly asynchronous base64 Blogging box c clang-format cmake compiler concurrency const_fn contravariant cos covariant cpp Customization cybersecurity DataStructure db Demo deserialization discrete doc DP Dynamic Example FFI flat_map FP Functional functions futures Fuwari GATs gccrs generics gitignore GUI hacking hashmap haskell heap interop invariant iterator justfile kernel LaTeX LFU linux MachineLearning Markdown math ML OnceLock optimization OS parallels perf physics pin postgresql release RPIT rust science Science serialization shift sin SmallProjects std String surrealdb swisstable synchronous tan traits triangulation utf16 utf8 Video x86_64 xilem zig
218 words
1 minutes
240702_shift_training
link
shift001(0 index를 삭제하기)
use std::ops::Shl;
// Newtype wrapper to enable Shl implementation
struct ShiftVec<T>(Vec<T>);
impl<T: Copy> Shl<usize> for ShiftVec<T> {
type Output = ShiftVec<T>;
fn shl(mut self, rhs: usize) -> Self::Output {
// Left shift: remove elements from front
for _ in 0..rhs {
if !self.0.is_empty() {
self.0.remove(0);
}
}
self
}
}
fn main() {
let my_arr = ShiftVec(vec![1, 2, 3, 4, 5]);
println!("before arr : {:?}", my_arr.0);
let res = my_arr << 1;
println!("after arr : {:?}", res.0);
}- result
before arr : [1, 2, 3, 4, 5]
after arr : [2, 3, 4, 5]shift002(0 index에 0추가하기)
use std::ops::Shl;
// Newtype wrapper to enable Shl implementation
struct ShiftVec<T>(Vec<T>);
impl<T: Copy + Default> Shl<usize> for ShiftVec<T> {
type Output = ShiftVec<T>;
fn shl(mut self, rhs: usize) -> Self::Output {
// Left shift: add zeros to front (push_front)
for _ in 0..rhs {
// insert 0 at the beginning (push_front equivalent)
self.0.insert(0, T::default());
}
self
}
}
fn main() {
let my_arr = ShiftVec(vec![1i32, 2, 3, 4, 5]);
println!("before arr : {:?}", my_arr.0);
let res = my_arr << 1;
println!("after arr : {:?}", res.0);
}- result
before arr : [1, 2, 3, 4, 5]
after arr : [0, 1, 2, 3, 4, 5]240702_shift_training
https://younghakim7.github.io/blog/posts/240702_shift_training/