<For Loop Part>
[예제 코드_1]
fn main() {
let names = vec![
String::from("John"),
String::from("Jane"),
];
for name in names {
println!("{}", name);
}
}
[실행 결과]
John
Jane
[예제 코드_2]
fn main() {
let names = vec![
String::from("John"),
String::from("Jane"),
];
for name in names {
println!("{}", name);
}
println!("Names: {:?}", names);
}
[실행 결과]
error[E0382]: borrow of moved value: `names`
--> src/main.rs:11:29
[예제 코드_3]
fn main() {
let names = vec![
String::from("John"),
String::from("Jane"),
];
for name in names.into_iter() {
println!("{}", name);
}
println!("Names: {:?}", names);
}
[실행 결과]
error[E0382]: borrow of moved value: `names`
--> src/main.rs:11:29
[예제 코드_4]
fn main() {
let names = vec![
String::from("John"),
String::from("Jane"),
];
let names_iter = names.into_iter();
for name in names_iter {
println!("{}", name);
}
println!("Names: {:?}", names);
}
[실행 결과]
error[E0382]: borrow of moved value: `names`
--> src/main.rs:11:29
[예제 코드_5]
fn main() {
let names = vec![
String::from("John"),
String::from("Jane"),
];
for name in names.iter() {
println!("{}", name);
}
println!("Names: {:?}", names);
}
[실행 코드]
John
Jane
Names: ["John", "Jane"]
[예제 코드_6]
fn main() {
let names = vec![
String::from("John"),
String::from("Jane"),
];
for name in &names {
println!("{}", name);
}
println!("Names: {:?}", names);
}
[실행 결과]
John
Jane
Names: ["John", "Jane"]
[예제 코드_7]
fn main() {
let names = vec![
String::from("John"),
String::from("Jane"),
];
println!("Names: {:?}", names);
for name in names {
println!("{}", name);
}
}
[예제 코드]
Names: ["John", "Jane"]
John
Jane
[예제 코드_8]
fn main() {
let names = vec![
String::from("John"),
String::from("Jane"),
];
for name in names.clone() {
println!("{}", name);
}
println!("Names: {:?}", names);
}
[실행 결과]
John
Jane
Names: ["John", "Jane"]
[예제 코드_9]
fn main() {
let names = vec![
String::from("John"),
String::from("Jane"),
];
names.for_each(|name|
println!("{}", name));
println!("{:?}", names);
}
[실행 결과]
error[E0599]: `Vec<String>` is not an iterator
--> src/main.rs:7:11
[예제 코드_10]
fn main() {
let names = vec![
String::from("John"),
String::from("Jane"),
];
names
.iter()
.for_each(|name|
println!("{}", name));
println!("{:?}", names);
}
[실행 결과]
John
Jane
["John", "Jane"]
[예제 코드_11]
fn main() {
let names = vec![
String::from("John"),
String::from("Jane"),
];
names
.into_iter()
.for_each(|name|
println!("{}", name));
println!("{:?}", names);
}
[실행 결과]
error[E0382]: borrow of moved value: `names`
--> src/main.rs:12:22
[예제 코드_12]
fn main() {
let names = vec![
String::from("John"),
String::from("Jane"),
];
println!("{:?}", names);
names
.into_iter()
.for_each(|name|
println!("{}", name));
}
[실행 결과]
["John", "Jane"]
John
Jane
[예제 코드_13]
fn main() {
let names = vec![
String::from("John"),
String::from("Jane"),
];
names.clone()
.into_iter()
.for_each(|name|
println!("{}", name));
println!("{:?}", names);
}
[실행 결과]
John
Jane
["John", "Jane"]
<참조 1> https://itnext.io/rust-ownership-50-code-examples-96203fcf79ea
'Rust' 카테고리의 다른 글
[Rust] Ownership(소유권) 기초 예제 : &str (37일차) (0) | 2023.01.20 |
---|---|
[Rust] Deref(역참조) 예제 (36일차) (0) | 2023.01.19 |
[Rust] Ownership(소유권) 기초 예제 : Vec<String> (34일차) (0) | 2023.01.16 |
[Rust] Ownership(소유권) 기초 예제 : Struct (33일차) (0) | 2023.01.15 |
댓글