mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-05-28 21:27:37 +02:00
ko: refresh translation for traits (#926)
Part of #925. Apply suggestions from code review Co-authored-by: Jiyong Park <55639800+jiyongp@users.noreply.github.com>
This commit is contained in:
parent
677e1feae1
commit
e6a28fbeaf
497
po/ko.po
497
po/ko.po
@ -8337,39 +8337,143 @@ msgstr "트레잇은 타입을 추상화 하는데 사용됩니다. 인터페이
|
||||
#: src/traits.md:5
|
||||
msgid ""
|
||||
"```rust,editable\n"
|
||||
"trait Greet {\n"
|
||||
" fn say_hello(&self);\n"
|
||||
"trait Pet {\n"
|
||||
" fn name(&self) -> String;\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"struct Dog {\n"
|
||||
" name: String,\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"struct Cat; // No name, cats won't respond to it anyway.\n"
|
||||
"struct Cat;\n"
|
||||
"\n"
|
||||
"impl Greet for Dog {\n"
|
||||
" fn say_hello(&self) {\n"
|
||||
" println!(\"Wuf, my name is {}!\", self.name);\n"
|
||||
"impl Pet for Dog {\n"
|
||||
" fn name(&self) -> String {\n"
|
||||
" self.name.clone()\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"impl Greet for Cat {\n"
|
||||
" fn say_hello(&self) {\n"
|
||||
" println!(\"Miau!\");\n"
|
||||
"impl Pet for Cat {\n"
|
||||
" fn name(&self) -> String {\n"
|
||||
" String::from(\"The cat\") // No name, cats won't respond to it "
|
||||
"anyway.\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn greet<P: Pet>(pet: &P) {\n"
|
||||
" println!(\"Who's a cutie? {} is!\", pet.name());\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let pets: Vec<Box<dyn Greet>> = vec![\n"
|
||||
" Box::new(Dog { name: String::from(\"Fido\") }),\n"
|
||||
" Box::new(Cat),\n"
|
||||
" ];\n"
|
||||
" for pet in pets {\n"
|
||||
" pet.say_hello();\n"
|
||||
" }\n"
|
||||
" let fido = Dog { name: \"Fido\".into() };\n"
|
||||
" greet(&fido);\n"
|
||||
"\n"
|
||||
" let captain_floof = Cat;\n"
|
||||
" greet(&captain_floof);\n"
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"trait Pet {\n"
|
||||
" fn name(&self) -> String;\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"struct Dog {\n"
|
||||
" name: String,\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"struct Cat;\n"
|
||||
"\n"
|
||||
"impl Pet for Dog {\n"
|
||||
" fn name(&self) -> String {\n"
|
||||
" self.name.clone()\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"impl Pet for Cat {\n"
|
||||
" fn name(&self) -> String {\n"
|
||||
" String::from(\"The cat\") // No name, cats won't respond to it "
|
||||
"anyway.\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn greet<P: Pet>(pet: &P) {\n"
|
||||
" println!(\"Who's a cutie? {} is!\", pet.name());\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let fido = Dog { name: \"Fido\".into() };\n"
|
||||
" greet(&fido);\n"
|
||||
"\n"
|
||||
" let captain_floof = Cat;\n"
|
||||
" greet(&captain_floof);\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/traits/default-methods.md:32
|
||||
msgid ""
|
||||
"* Traits may specify pre-implemented (default) methods and methods that "
|
||||
"users are required to\n"
|
||||
" implement themselves. Methods with default implementations can rely on "
|
||||
"required methods.\n"
|
||||
"\n"
|
||||
"* Move method `not_equal` to a new trait `NotEqual`.\n"
|
||||
"\n"
|
||||
"* Make `NotEqual` a super trait for `Equal`.\n"
|
||||
" ```rust,editable,compile_fail\n"
|
||||
" trait NotEqual: Equals {\n"
|
||||
" fn not_equal(&self, other: &Self) -> bool {\n"
|
||||
" !self.equal(other)\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
" ```\n"
|
||||
"\n"
|
||||
"* Provide a blanket implementation of `NotEqual` for `Equal`.\n"
|
||||
" ```rust,editable,compile_fail\n"
|
||||
" trait NotEqual {\n"
|
||||
" fn not_equal(&self, other: &Self) -> bool;\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" impl<T> NotEqual for T where T: Equals {\n"
|
||||
" fn not_equal(&self, other: &Self) -> bool {\n"
|
||||
" !self.equal(other)\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
" ```\n"
|
||||
" * With the blanket implementation, you no longer need `NotEqual` as a "
|
||||
"super trait for `Equal`.\n"
|
||||
" "
|
||||
msgstr ""
|
||||
"* 트레잇은 사전 구현된 (기본값) 메서드와 사용자가 직접 구현해야 하는\n"
|
||||
" 메서드를 지정할 수 있습니다. 기본 구현이 있는 메서드는 필수 메서드를 사용"
|
||||
"할 수 있습니다.\n"
|
||||
"\n"
|
||||
"* `not_equal` 메서드를 새로운 트레잇인 `NotEqual`로 이동합니다.\n"
|
||||
"\n"
|
||||
"* `NotEqual`을 `Equal`의 슈퍼 트레잇으로 만듭니다.\n"
|
||||
" ```rust,editable,compile_fail\n"
|
||||
" trait NotEqual: Equals {\n"
|
||||
" fn not_equal(&self, other: &Self) -> bool {\n"
|
||||
" !self.equal(other)\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
" ```\n"
|
||||
"\n"
|
||||
"* `Equal`에 `NotEqual`의 포괄적 구현을 제공합니다.\n"
|
||||
" ```rust,editable,compile_fail\n"
|
||||
" trait NotEqual {\n"
|
||||
" fn not_equal(&self, other: &Self) -> bool;\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" impl<T> NotEqual for T where T: Equals {\n"
|
||||
" fn not_equal(&self, other: &Self) -> bool {\n"
|
||||
" !self.equal(other)\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
" ```\n"
|
||||
" * 포괄적 구현을 사용하면 더 이상 `NotEqual`이 `Equal`의 슈퍼 트레잇으로 필"
|
||||
"요하지 않습니다.\n"
|
||||
" "
|
||||
|
||||
#: src/traits.md:41
|
||||
msgid ""
|
||||
@ -8423,6 +8527,21 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"#[derive(Debug, Clone, PartialEq, Eq, Default)]\n"
|
||||
"struct Player {\n"
|
||||
" name: String,\n"
|
||||
" strength: u8,\n"
|
||||
" hit_points: u8,\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let p1 = Player::default();\n"
|
||||
" let p2 = p1.clone();\n"
|
||||
" println!(\"Is {:?}\\nequal to {:?}?\\nThe answer is {}!\", &p1, &p2,\n"
|
||||
" if p1 == p2 { \"yes\" } else { \"no\" });\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/traits/default-methods.md:1
|
||||
msgid "# Default Methods"
|
||||
@ -8459,6 +8578,30 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"trait Equals {\n"
|
||||
" fn equal(&self, other: &Self) -> bool;\n"
|
||||
" fn not_equal(&self, other: &Self) -> bool {\n"
|
||||
" !self.equal(other)\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"#[derive(Debug)]\n"
|
||||
"struct Centimeter(i16);\n"
|
||||
"\n"
|
||||
"impl Equals for Centimeter {\n"
|
||||
" fn equal(&self, other: &Centimeter) -> bool {\n"
|
||||
" self.0 == other.0\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let a = Centimeter(10);\n"
|
||||
" let b = Centimeter(20);\n"
|
||||
" println!(\"{a:?} equals {b:?}: {}\", a.equal(&b));\n"
|
||||
" println!(\"{a:?} not_equals {b:?}: {}\", a.not_equal(&b));\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/traits/important-traits.md:1
|
||||
msgid "# Important Traits"
|
||||
@ -8519,6 +8662,30 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"struct Fibonacci {\n"
|
||||
" curr: u32,\n"
|
||||
" next: u32,\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"impl Iterator for Fibonacci {\n"
|
||||
" type Item = u32;\n"
|
||||
"\n"
|
||||
" fn next(&mut self) -> Option<Self::Item> {\n"
|
||||
" let new_next = self.curr + self.next;\n"
|
||||
" self.curr = self.next;\n"
|
||||
" self.next = new_next;\n"
|
||||
" Some(self.curr)\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let fib = Fibonacci { curr: 0, next: 1 };\n"
|
||||
" for (i, n) in fib.enumerate().take(5) {\n"
|
||||
" println!(\"fib({i}): {n}\");\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/traits/iterator.md:32
|
||||
msgid ""
|
||||
@ -8534,6 +8701,37 @@ msgstr ""
|
||||
"* `Iterator` 트레잇은 컬렉션에 대해 다양한 함수형 프로그래밍 연산 (`map`, `filter`, `reduce` 등)을 구현합니다. 이 연산들에 대한 자세한 설명은 `Iterator` 트레잇의 API 레퍼런스에서 찾을 수 있습니다. 러스트에서 이러한 함수형 연산들은 절차형으로 구현된 코드와 동일한 성능을 보여줍니다.\n"
|
||||
" "
|
||||
|
||||
#: src/traits/iterator.md:32
|
||||
msgid ""
|
||||
"* The `Iterator` trait implements many common functional programming "
|
||||
"operations over collections \n"
|
||||
" (e.g. `map`, `filter`, `reduce`, etc). This is the trait where you can "
|
||||
"find all the documentation\n"
|
||||
" about them. In Rust these functions should produce the code as efficient "
|
||||
"as equivalent imperative\n"
|
||||
" implementations.\n"
|
||||
" \n"
|
||||
"* `IntoIterator` is the trait that makes for loops work. It is implemented "
|
||||
"by collection types such as\n"
|
||||
" `Vec<T>` and references to them such as `&Vec<T>` and `&[T]`. Ranges also "
|
||||
"implement it. This is why\n"
|
||||
" you can iterate over a vector with `for i in some_vec { .. }` but\n"
|
||||
" `some_vec.next()` doesn't exist."
|
||||
msgstr ""
|
||||
"* `Iterator` 트레잇은 컬렉션에 대한 여러 일반적인 함수 프로그래밍 작업(예: "
|
||||
"`map`, `filter`, `reduce` 등)을\n"
|
||||
" 구현합니다. 이는 관련 문서를 모두 찾을 수 있는\n"
|
||||
" 트레잇입니다. Rust에서는 이러한 함수가, 이와 동일한 일을 하는 명령형 구현만큼 효율적인 코"
|
||||
"드를\n"
|
||||
" 생성합니다.\n"
|
||||
" \n"
|
||||
"* `IntoIterator`는 루프를 작동하게 만드는 트레잇입니다. 이는 `Vec<T>`와 같은 "
|
||||
"컬렉션 유형과\n"
|
||||
" `&Vec<T>` 및 `&[T]`와 같은 이에 대한 참조에 의해 구현됩니다. 범위도 이를 구"
|
||||
"현합니다. 이런 이유로\n"
|
||||
" `for i in some_vec { .. }`를 사용하여 벡터를 반복할 수 있지만\n"
|
||||
" `some_vec.next()`는 존재하지 않습니다."
|
||||
|
||||
#: src/traits/from-iterator.md:1
|
||||
msgid "# FromIterator"
|
||||
msgstr "# FromIterator"
|
||||
@ -8554,6 +8752,15 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"fn main() {\n"
|
||||
" let primes = vec![2, 3, 5, 7];\n"
|
||||
" let prime_squares = primes\n"
|
||||
" .into_iter()\n"
|
||||
" .map(|prime| prime * prime)\n"
|
||||
" .collect::<Vec<_>>();\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/traits/from-iterator.md:17
|
||||
msgid ""
|
||||
@ -8595,6 +8802,15 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"fn main() {\n"
|
||||
" let s = String::from(\"hello\");\n"
|
||||
" let addr = std::net::Ipv4Addr::from([127, 0, 0, 1]);\n"
|
||||
" let one = i16::from(true);\n"
|
||||
" let bigger = i32::from(123i16);\n"
|
||||
" println!(\"{s}, {addr}, {one}, {bigger}\");\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/traits/from-into.md:15
|
||||
msgid "[`Into`][2] is automatically implemented when [`From`][1] is implemented:"
|
||||
@ -8612,6 +8828,15 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"fn main() {\n"
|
||||
" let s: String = \"hello\".into();\n"
|
||||
" let addr: std::net::Ipv4Addr = [127, 0, 0, 1].into();\n"
|
||||
" let one: i16 = true.into();\n"
|
||||
" let bigger: i32 = 123i16.into();\n"
|
||||
" println!(\"{s}, {addr}, {one}, {bigger}\");\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/traits/from-into.md:29
|
||||
msgid ""
|
||||
@ -8652,6 +8877,23 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"use std::io::{BufRead, BufReader, Read, Result};\n"
|
||||
"\n"
|
||||
"fn count_lines<R: Read>(reader: R) -> usize {\n"
|
||||
" let buf_reader = BufReader::new(reader);\n"
|
||||
" buf_reader.lines().count()\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() -> Result<()> {\n"
|
||||
" let slice: &[u8] = b\"foo\\nbar\\nbaz\\n\";\n"
|
||||
" println!(\"lines in slice: {}\", count_lines(slice));\n"
|
||||
"\n"
|
||||
" let file = std::fs::File::open(std::env::current_exe()?)?;\n"
|
||||
" println!(\"lines in file: {}\", count_lines(file));\n"
|
||||
" Ok(())\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/traits/read-write.md:23
|
||||
msgid "Similarly, [`Write`][3] lets you abstract over `u8` sinks:"
|
||||
@ -8676,6 +8918,22 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"use std::io::{Result, Write};\n"
|
||||
"\n"
|
||||
"fn log<W: Write>(writer: &mut W, msg: &str) -> Result<()> {\n"
|
||||
" writer.write_all(msg.as_bytes())?;\n"
|
||||
" writer.write_all(\"\\n\".as_bytes())\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() -> Result<()> {\n"
|
||||
" let mut buffer = Vec::new();\n"
|
||||
" log(&mut buffer, \"Hello\")?;\n"
|
||||
" log(&mut buffer, \"World\")?;\n"
|
||||
" println!(\"Logged: {:?}\", buffer);\n"
|
||||
" Ok(())\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/traits/operators.md:1
|
||||
msgid "# `Add`, `Mul`, ..."
|
||||
@ -8706,6 +8964,24 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"#[derive(Debug, Copy, Clone)]\n"
|
||||
"struct Point { x: i32, y: i32 }\n"
|
||||
"\n"
|
||||
"impl std::ops::Add for Point {\n"
|
||||
" type Output = Self;\n"
|
||||
"\n"
|
||||
" fn add(self, other: Self) -> Self {\n"
|
||||
" Self {x: self.x + other.x, y: self.y + other.y}\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let p1 = Point { x: 10, y: 20 };\n"
|
||||
" let p2 = Point { x: 100, y: 200 };\n"
|
||||
" println!(\"{:?} + {:?} = {:?}\", p1, p2, p1 + p2);\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/traits/operators.md:26 src/traits/drop.md:34
|
||||
msgid "Discussion points:"
|
||||
@ -8765,6 +9041,32 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"struct Droppable {\n"
|
||||
" name: &'static str,\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"impl Drop for Droppable {\n"
|
||||
" fn drop(&mut self) {\n"
|
||||
" println!(\"Dropping {}\", self.name);\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let a = Droppable { name: \"a\" };\n"
|
||||
" {\n"
|
||||
" let b = Droppable { name: \"b\" };\n"
|
||||
" {\n"
|
||||
" let c = Droppable { name: \"c\" };\n"
|
||||
" let d = Droppable { name: \"d\" };\n"
|
||||
" println!(\"Exiting block B\");\n"
|
||||
" }\n"
|
||||
" println!(\"Exiting block A\");\n"
|
||||
" }\n"
|
||||
" drop(a);\n"
|
||||
" println!(\"Exiting main\");\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/traits/drop.md:36
|
||||
msgid ""
|
||||
@ -8821,6 +9123,38 @@ msgid ""
|
||||
"\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"#[derive(Debug, Default)]\n"
|
||||
"struct Derived {\n"
|
||||
" x: u32,\n"
|
||||
" y: String,\n"
|
||||
" z: Implemented,\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"#[derive(Debug)]\n"
|
||||
"struct Implemented(String);\n"
|
||||
"\n"
|
||||
"impl Default for Implemented {\n"
|
||||
" fn default() -> Self {\n"
|
||||
" Self(\"John Smith\".into())\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let default_struct: Derived = Default::default();\n"
|
||||
" println!(\"{default_struct:#?}\");\n"
|
||||
"\n"
|
||||
" let almost_default_struct = Derived {\n"
|
||||
" y: \"Y is set!\".into(),\n"
|
||||
" ..Default::default()\n"
|
||||
" };\n"
|
||||
" println!(\"{almost_default_struct:#?}\");\n"
|
||||
"\n"
|
||||
" let nothing: Option<Derived> = None;\n"
|
||||
" println!(\"{:#?}\", nothing.unwrap_or_default());\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"```"
|
||||
|
||||
#: src/traits/default.md:40
|
||||
msgid ""
|
||||
@ -8930,7 +9264,7 @@ msgstr "제네릭을 이용하다 보면 타입이 어떤 트레잇을 구현하
|
||||
msgid "You can do this with `T: Trait` or `impl Trait`:"
|
||||
msgstr "`T: Trait` 혹은 `impl Trait`를 사용하면 됩니다:"
|
||||
|
||||
#: src/generics/trait-bounds.md:8
|
||||
#: src/traits/trait-bounds.md:8
|
||||
msgid ""
|
||||
"```rust,editable\n"
|
||||
"fn duplicate<T: Clone>(a: T) -> (T, T) {\n"
|
||||
@ -8957,6 +9291,30 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"fn duplicate<T: Clone>(a: T) -> (T, T) {\n"
|
||||
" (a.clone(), a.clone())\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"// 다음에 대한 문법 슈가:\n"
|
||||
"// fn add_42_millions<T: Into<i32>>(x: T) -> i32 {\n"
|
||||
"fn add_42_millions(x: impl Into<i32>) -> i32 {\n"
|
||||
" x.into() + 42_000_000\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"// struct NotClonable;\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let foo = String::from(\"foo\");\n"
|
||||
" let pair = duplicate(foo);\n"
|
||||
" println!(\"{pair:?}\");\n"
|
||||
"\n"
|
||||
" let many = add_42_millions(42_i8);\n"
|
||||
" println!(\"{many}\");\n"
|
||||
" let many_more = add_42_millions(10_000_000);\n"
|
||||
" println!(\"{many_more}\");\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/generics/trait-bounds.md:35
|
||||
msgid "Show a `where` clause, students will encounter it when reading code."
|
||||
@ -9004,7 +9362,7 @@ msgid ""
|
||||
"arguments and return values:"
|
||||
msgstr "트레잇 바운드와 유사하게 `impl Trait` 문법은 함수의 인자와 반환값에도 적용 가능합니다:"
|
||||
|
||||
#: src/generics/impl-trait.md:6
|
||||
#: src/traits/impl-trait.md:6
|
||||
msgid ""
|
||||
"```rust,editable\n"
|
||||
"use std::fmt::Display;\n"
|
||||
@ -9019,6 +9377,18 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"use std::fmt::Display;\n"
|
||||
"\n"
|
||||
"fn get_x(name: impl Display) -> impl Display {\n"
|
||||
" format!(\"Hello {name}\")\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let x = get_x(\"foo\");\n"
|
||||
" println!(\"{x}\");\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/generics/impl-trait.md:19
|
||||
msgid ""
|
||||
@ -9064,7 +9434,7 @@ msgid ""
|
||||
"[`FnOnce`](https://doc.rust-lang.org/std/ops/trait.FnOnce.html) traits:"
|
||||
msgstr "클로저 혹은 람다표현식은 익명타입입니다. 이들은 [`Fn`](https://doc.rust-lang.org/std/ops/trait.Fn.html),[`FnMut`](https://doc.rust-lang.org/std/ops/trait.FnMut.html), [`FnOnce`](https://doc.rust-lang.org/std/ops/trait.FnOnce.html) 라는 특별한 트레잇을 구현합니다:"
|
||||
|
||||
#: src/generics/closures.md:8
|
||||
#: src/traits/closures.md:8
|
||||
msgid ""
|
||||
"```rust,editable\n"
|
||||
"fn apply_with_log(func: impl FnOnce(i32) -> i32, input: i32) -> i32 {\n"
|
||||
@ -9081,6 +9451,20 @@ msgid ""
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"fn apply_with_log(func: impl FnOnce(i32) -> i32, input: i32) -> i32 {\n"
|
||||
" println!(\"Calling function on {input}\");\n"
|
||||
" func(input)\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let add_3 = |x| x + 3;\n"
|
||||
" let mul_5 = |x| x * 5;\n"
|
||||
"\n"
|
||||
" println!(\"add_3: {}\", apply_with_log(add_3, 10));\n"
|
||||
" println!(\"mul_5: {}\", apply_with_log(mul_5, 20));\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/generics/closures.md:25
|
||||
msgid "If you have an `FnOnce`, you may only call it once. It might consume captured values."
|
||||
@ -9163,21 +9547,84 @@ msgstr "# 트레잇 객체"
|
||||
msgid "We've seen how a function can take arguments which implement a trait:"
|
||||
msgstr "함수가 특정 트레잇을 구현하는 인자를 입력으로 받도록 하는 방법에 대해 설명했었습니다:"
|
||||
|
||||
#: src/generics/trait-objects.md:5
|
||||
|
||||
#: src/traits/trait-objects.md:3
|
||||
msgid ""
|
||||
"Trait objects allow for values of different types, for instance in a "
|
||||
"collection:"
|
||||
msgstr "트레잇 객체는 타입이 다른 값(예를 들어 컬렉션에 속한 각 값)들을 가질 수 있습니다."
|
||||
|
||||
#: src/traits/trait-objects.md:5
|
||||
msgid ""
|
||||
"```rust,editable\n"
|
||||
"use std::fmt::Display;\n"
|
||||
"trait Pet {\n"
|
||||
" fn name(&self) -> String;\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn print<T: Display>(x: T) {\n"
|
||||
" println!(\"Your value: {x}\");\n"
|
||||
"struct Dog {\n"
|
||||
" name: String,\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"struct Cat;\n"
|
||||
"\n"
|
||||
"impl Pet for Dog {\n"
|
||||
" fn name(&self) -> String {\n"
|
||||
" self.name.clone()\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"impl Pet for Cat {\n"
|
||||
" fn name(&self) -> String {\n"
|
||||
" String::from(\"The cat\") // No name, cats won't respond to it "
|
||||
"anyway.\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" print(123);\n"
|
||||
" print(\"Hello\");\n"
|
||||
" let pets: Vec<Box<dyn Pet>> = vec![\n"
|
||||
" Box::new(Cat),\n"
|
||||
" Box::new(Dog { name: String::from(\"Fido\") }),\n"
|
||||
" ];\n"
|
||||
" for pet in pets {\n"
|
||||
" println!(\"Hello {}!\", pet.name());\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"```"
|
||||
msgstr ""
|
||||
"```rust,editable\n"
|
||||
"trait Pet {\n"
|
||||
" fn name(&self) -> String;\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"struct Dog {\n"
|
||||
" name: String,\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"struct Cat;\n"
|
||||
"\n"
|
||||
"impl Pet for Dog {\n"
|
||||
" fn name(&self) -> String {\n"
|
||||
" self.name.clone()\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"impl Pet for Cat {\n"
|
||||
" fn name(&self) -> String {\n"
|
||||
" String::from(\"The cat\") // No name, cats won't respond to it "
|
||||
"anyway.\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"fn main() {\n"
|
||||
" let pets: Vec<Box<dyn Pet>> = vec![\n"
|
||||
" Box::new(Cat),\n"
|
||||
" Box::new(Dog { name: String::from(\"Fido\") }),\n"
|
||||
" ];\n"
|
||||
" for pet in pets {\n"
|
||||
" println!(\"Hello {}!\", pet.name());\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"```"
|
||||
|
||||
#: src/generics/trait-objects.md:18
|
||||
msgid "However, how can we store a collection of mixed types which implement `Display`?"
|
||||
|
Loading…
x
Reference in New Issue
Block a user