<&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
'Rust' 카테고리의 다른 글
[Rust] RefCell 예제 (39일차) (0) | 2023.01.22 |
---|---|
[Rust] Rc<T> 참조 카운팅 스마트 포인터 예제 (38일자) (0) | 2023.01.21 |
[Rust] Deref(역참조) 예제 (36일차) (0) | 2023.01.19 |
[Rust] Ownership(소유권) 기초 예제 : For Loop (35일차) (0) | 2023.01.18 |
댓글