🦉
Programming Notes
  • My Programming Notes
  • CKA Exam Preparation
    • Certified Kubernetes Administrator
    • Setup Minikube
    • Network Design Principles
    • Role-Based Access Control (RBAC)
    • Namespace
    • Resource Quota
    • Pod
    • Deployment
    • Deployment: Rollout
    • ConfigMap
    • Service
    • Service: kubectl expose
    • Pod: Resources Management
    • Pod & Container: Quality of Service Class
    • Pod & Container: Probes
    • Limit Range
    • Scaling: Manual
    • Scaling: Horizontal Pod Autoscaler
    • Persistent Volume & Claim
    • Secret
    • Ingress: Routing
    • Ingress: TLS
    • Ingress: Rate Limit
    • Ingress: Basic Auth
    • Ingress: CRD (Custom Resource Definition)
    • Job
    • CronJob
    • Mutli-Node Cluster
  • Golang
    • Generics
    • Context
    • Goroutines and Channels in Go
    • Goroutine: Concurrency vs Parallelism
    • Goroutine: Performance & Tradeoffs
    • JSON: omitzero
  • Rust
    • Arrays & Slices
    • Closures
    • Generics & Traits
    • Iterators
    • Run Code Simultaneously
    • String vs &str
    • Tests
    • Rustlings Exercises
      • Variables
      • Functions
      • If
      • Primitive Types
      • Vectors
      • Move Semantics
      • Structs
      • Enums and Matching Pattern
      • Strings
      • Modules
      • Hashmaps
      • Options
      • Error Handling
      • Generics
      • Traits
      • Lifetimes
      • Tests
      • Iterators
      • Smart Pointers
      • Threads
      • Macros
      • Quiz 1
      • Quiz 2
      • Quiz 3
  • Software Engineering
    • CAP Theorem
    • Circuit Breaker
    • Decoupling
    • GraphQL: Query Caching
    • HMAC Signature
    • Idempotency
    • Monolith VS Microservice
    • OWASP Top 10 2021
    • PCI DSS
    • PostgreSQL: Partitioning
    • PostgreSQL: Replication
    • Protobuf & gRPC
    • Redis: Streams
    • Resource Scaling
    • Signed URL
    • SOLID
    • Stack VS Heap
    • Stateful VS Stateless
  • Site Reliability Engineering
    • Chaos Engineering
    • Distributed Tracing
    • Kubernetes (k8s)
    • SLA, SLO, and SLI Metrics
    • Site Reliability Engineer
  • Others
    • FFMPEG Cheat sheet
Powered by GitBook
On this page
  • String
  • &str
  • Conversions Between String and &str
  • Examples
  • References
  1. Rust

String vs &str

String

  • Owned by the variable, meaning the variable controls the memory.

  • Can be mutated.

  • Ideal for strings whose size can change or need to persist beyond the scope of their original reference.

&str

  • Borrowed, immutable string slice.

  • Represents a view into a string, rather than owning the data.

  • Usually refers to a segment of a String or a string literal.

  • Immutable by default.

Conversions Between String and &str

Convert &str to String: Use the to_string or String::from methods.

let slice: &str = "Hello";
let owned: String = slice.to_string();
let owned2: String = String::from(slice);

Convert String to &str: Borrow a string using & or .as_str().

let owned: String = String::from("Hello");
let slice: &str = &owned;
let slice2: &str = owned.as_str();

Examples

fn main() {
    let x = "hello ";
    println!("\"{}\"", x.trim()); // ok
    println!("\"{}\"", x + "world"); // error
}
  • In above example the x.trim function will work because it doesn't manipulate the string.

  • Instead it return a slice of original string.

  • But x + "world" got compile error because you cannot mutate the variable x: &str.

fn main() {
    let x = "hello ".to_string();
    println!("\"{}\"", x + "world"); // ok
}
  • Above example will work because x is converted to String and can be mutated.

  • In other words, any function or method that manipulate the value will not work using &str but will work using String type.

References

PreviousRun Code SimultaneouslyNextTests

Last updated 4 months ago

https://doc.rust-lang.org/book/ch08-02-strings.html#what-is-a-string