# 方法语法

method-syntax.md

baz(bar(foo));

foo.bar().baz();

## 方法调用

struct Circle {
x: f64,
y: f64,
}

impl Circle {
fn area(&self) -> f64 {
}
}

fn main() {
let c = Circle { x: 0.0, y: 0.0, radius: 2.0 };
println!("{}", c.area());
}

struct Circle {
x: f64,
y: f64,
}

impl Circle {
fn reference(&self) {
println!("taking self by reference!");
}

fn mutable_reference(&mut self) {
println!("taking self by mutable reference!");
}

fn takes_ownership(self) {
println!("taking ownership of self!");
}
}

struct Circle {
x: f64,
y: f64,
}

impl Circle {
fn reference(&self) {
println!("taking self by reference!");
}
}

impl Circle {
fn mutable_reference(&mut self) {
println!("taking self by mutable reference!");
}
}

impl Circle {
fn takes_ownership(self) {
println!("taking ownership of self!");
}
}

## 链式方法调用（Chaining method calls）

struct Circle {
x: f64,
y: f64,
}

impl Circle {
fn area(&self) -> f64 {
}

fn grow(&self, increment: f64) -> Circle {
}
}

fn main() {
let c = Circle { x: 0.0, y: 0.0, radius: 2.0 };
println!("{}", c.area());

let d = c.grow(2.0).area();
println!("{}", d);
}

# struct Circle;
# impl Circle {
fn grow(&self, increment: f64) -> Circle {
# Circle } }

## 关联函数（Associated functions）

struct Circle {
x: f64,
y: f64,
}

impl Circle {
fn new(x: f64, y: f64, radius: f64) -> Circle {
Circle {
x: x,
y: y,
}
}
}

fn main() {
let c = Circle::new(0.0, 0.0, 2.0);
}

## 创建者模式（Builder Pattern）

struct Circle {
x: f64,
y: f64,
}

impl Circle {
fn area(&self) -> f64 {
}
}

struct CircleBuilder {
x: f64,
y: f64,
}

impl CircleBuilder {
fn new() -> CircleBuilder {
CircleBuilder { x: 0.0, y: 0.0, radius: 1.0, }
}

fn x(&mut self, coordinate: f64) -> &mut CircleBuilder {
self.x = coordinate;
self
}

fn y(&mut self, coordinate: f64) -> &mut CircleBuilder {
self.y = coordinate;
self
}

self
}

fn finalize(&self) -> Circle {
}
}

fn main() {
let c = CircleBuilder::new()
.x(1.0)
.y(2.0)
}