594 words
3 minutes
StringView_Unsafe_Rust_ver

link#



✅ Summary#

FeatureCRust
Low level
Safe by default
Unsafe allowed
Pointer mathunsafe
Memory bugs possiblemanyfew
Performancehighhigh
Kernel / exploit styleeasyunsafe needed
Modern safetynoyes
  • Rust = C + safety + ownership + optional unsafe

  • C = raw power, no guardrails

UnsafeRust(StringView)|🔝|#

// main.rs
struct StringView {
    data: *const u8,
    count: usize,
}

impl StringView {
    // equivalent to strlen
    fn c_strlen(mut s: *const u8) -> usize {
        let mut len = 0;

        unsafe {
            while *s != 0 {
                s = s.add(1);
                len += 1;
            }
        }

        len
    }

    // String_View sv(const char *cstr)
    fn sv(cstr: *const u8) -> Self {
        Self {
            data: cstr,
            count: Self::c_strlen(cstr),
        }
    }

    // void sv_chop_left(String_View *sv, size_t n)
    fn sv_chop_left(sv: *mut Self, mut n: usize) {
        unsafe {
            if n > (*sv).count {
                n = (*sv).count;
            }

            (*sv).count -= n;
            (*sv).data = (*sv).data.add(n);
        }
    }

    // void sv_chop_right(String_View *sv, size_t n)
    fn sv_chop_right(sv: *mut Self, mut n: usize) {
        unsafe {
            if n > (*sv).count {
                n = (*sv).count;
            }

            (*sv).count -= n;
        }
    }
}

fn main() {
    unsafe {
        // C string literal
        let cstr = b"Hello, World\0";

        let mut s = StringView::sv(cstr.as_ptr());

        StringView::sv_chop_right(&mut s, 3);
        StringView::sv_chop_left(&mut s, 2);

        // printf("%.*s\n", (int)s.count, s.data);
        let slice = std::slice::from_raw_parts(s.data, s.count);

        let text = std::str::from_utf8_unchecked(slice);

        println!("{}", text);
    }
}
  • result
llo, Wo
  • Here is a clear comparison table between C and Rust, focused on pointer, memory, struct, safety, and low-level behavior — based on your String_View / StringView example.
    • This table shows real differences in language design, not just syntax.

✅ C vs Rust comparison table|🔝|#

ConceptCRustExplanation
Struct definitiontypedef struct {}struct {}Similar, but Rust has ownership rules
Pointer to structString_View **mut StringView / &mut StringViewRust separates raw pointer vs reference
Member accesssv->countsv.count / (*ptr).countRust references auto-deref
Address of variable&s&mut s / &sRust distinguishes mutable / immutable
Mutable / constnonemut requiredRust must declare mutability
Pointer arithmeticallowedonly in unsafeRust forbids by default
Raw pointer readalways allowedunsafe requiredRust checks safety
strlenbuiltin librarymanual / unsafeRust has no null-string type
String literal"Hello"b"Hello\0" or "Hello"Rust strings are not C strings
Null-terminated stringdefaultnot defaultRust uses length + pointer
Array boundsnot checkedchecked (safe)Rust prevents overflow
Slicemanualbuilt-in &[T]Rust slices store len
Printing stringprintf("%.*s")slice + utf8Rust needs valid UTF-8
Function pointer argpointerreference or pointerRust prefers references
Pointer vs referencesamedifferent typesRust separates safety levels
Memory safetymanualenforcedRust prevents UB
Dangling pointerpossibleprevented (safe)unless unsafe
Use after freepossiblepreventedborrow checker
Double freepossiblepreventedownership
Buffer overflowpossiblepreventedsafe slice
Null pointercommonrareOption / reference
Struct copyshallow copymove / copy traitRust tracks ownership
Default mutabilitymutableimmutablemust use mut
Global variableseasyrestrictedRust safer
Inline pointer mathnormalunsafe onlykey difference
C style APInaturalneeds unsafeRust wraps C
Compile checksfewmanyRust compile-time safety
Runtime costlowlowRust = zero-cost abstraction
UB riskhighlowRust designed to avoid UB
FFI supportnativeexcellentRust works with C
Kernel style codenaturalunsafe neededbut possible
Low-level controlfullfull (unsafe)Rust still allows
Safe high-levelnoyesbig Rust advantage

✅ Pointer comparison (important)|🔝|#

ConceptCRust safeRust unsafe
pointerT *&T / &mut T*const T / *mut T
deref*pautomatic*p
pointer mathp + 1p.add(1)
nullallowednoallowed
UB checknonecompile-timenone

✅ Why Rust feels harder than C|🔝|#

Because Rust forces you to choose:

In CIn Rust
pointer alwayspointer OR reference
mutable defaultimmutable default
unsafe defaultsafe default
no ownershipownership required
manual memorychecked memory
  • Rust makes you prove correctness at compile time.

✅ Your example shows the key difference|🔝|#

  • C
// main.c
sv->data += n;
  • Rust unsafe
// main.rs
(*sv).data = (*sv).data.add(n);
  • Rust safe (not possible with raw pointer)
// main.rs
sv.data = &sv.data[n..];
  • Rust forces you to choose safety level.
StringView_Unsafe_Rust_ver
https://younghakim7.github.io/blog/posts/stringview_unsafe_rust_ver/
Author
YoungHa
Published at
2026-03-25