๋ฉ์๋
๋ฉ์๋๋ ํน์  ํ์
๊ณผ ์ฐ๊ฒฐ๋ ํจ์์
๋๋ค. ๋ฉ์๋์ self ์ธ์๊ฐ ๊ทธ ๋ฉ์๋๊ฐ ์ฐ๊ฒฐ๋ ์ธ์คํด์ค์ ํ์
์
๋๋ค:
struct Rectangle { width: u32, height: u32, } impl Rectangle { fn area(&self) -> u32 { self.width * self.height } fn inc_width(&mut self, delta: u32) { self.width += delta; } } fn main() { let mut rect = Rectangle { width: 10, height: 5 }; println!("old area: {}", rect.area()); rect.inc_width(5); println!("new area: {}", rect.area()); }
- ์ค๋๊ณผ ๋ด์ผ ๊ฐ์์์ ๋ ๋ง์ ๋ฉ์๋ ์ฌ์ฉ๋ฒ์ ๋ค๋ฃฐ ๊ฒ์ ๋๋ค.
 
- 
Rectangle::new์์ฑ์๋ฅผ ์ถ๊ฐํ๊ณ ์ด๋ฅผmain์์ ํธ์ถํฉ๋๋ค:fn new(width: u32, height: u32) -> Rectangle { Rectangle { width, height } } - 
_๊ธฐ์ ์ _์ผ๋ก ์ด์ผ๊ธฐ ํ์๋ฉด, ๋ฌ์คํธ๋ ์ปค์คํ  ์์ฑ์๋ฅผ ์ง์ํ์ง ์์ต๋๋ค. ์ ์  ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๊ตฌ์กฐ์ฒด๋ฅผ ์ด๊ธฐํ ํ๋ ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ์ ๋๋ค (๋ฌผ๋ก ์ด๊ฒ์ด ๊ฐ์ ๋์ง๋ ์์ต๋๋ค). ์ง์ง ์์ฑ์์ธ
Rectangle { width, height }๋ฅผ ์ง์  ํธ์ถํ ์๋ ์์ต๋๋ค. ์์ธํ ๋ด์ฉ์ Rustnomicon์ ์ฐธ์กฐํ์ธ์. - 
Rectangle::square(width: u32)์์ฑ์๋ฅผ ์ถ๊ฐํ์ฌ ์์ฑ์๊ฐ ์์์ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์์์ ๋ณด์ ์๋ค.