본문 바로가기
Rust

[Rust] Ownership(소유권) 기초 예제 : &str (37일차)

by 꾸압 2023. 1. 20.

 

<&str Part>

[예제 코드_1]

fn main() {
    let name: &'static str = "Rust";
    do_something(name);
    do_something(name);
}

fn do_something(name: &str) {
    println!("Hello, {:?}!", name);
}

 

[실행 결과]

Hello, Rust!
Hello, Rust!

 


 

[예제 코드_2]

fn main() {
    let name: &'static str = "Rust";
    do_something(name.clone());
    do_something(name.clone());
}

fn do_something(name: &str) {
    println!("Hello, {:?}!", name);
}

 

[실행 결과]

Hello, Rust!
Hello, Rust!

 


 

[예제 코드_3]

fn main() {
    let name: &'static str = "Rust";
    do_something(<&str>::clone(&name));
    do_something(<&str>::clone(&name));
}

fn do_something(name: &str) {
    println!("Hello, {:?}!", name);
}

 

[실행 결과]

Hello, Rust!
Hello, Rust!

 


 

<참조 1> https://itnext.io/rust-ownership-50-code-examples-96203fcf79ea

 

 

댓글