rust如何连接oracle数据库,Rust方法

方法

方法是附着于对象的函数。这些方法通过self关键字访问对象的数据和它的其他方法。 方法是一个实现了impl块的定义。

struct Point {

x: f64,

y: f64,

}

// Implementation block, all `Point` methods go in here

impl Point {

// This is a static method

// Static methods don't need to be called by an instance

// These methods are generally used as constructors

fn origin() -> Point {

Point { x: 0.0, y: 0.0 }

}

// Another static method, that takes two arguments

fn new(x: f64, y: f64) -> Point {

Point { x: x, y: y }

}

}

struct Rectangle {

p1: Point,

p2: Point,

}

impl Rectangle {

// This is an instance method

// `&self` is sugar for `self: &Self`, where `Self` is the type of the

// caller object. In this case `Self` = `Rectangle`

fn area(&self) -> f64 {

// `self` gives access to the struct fields via the dot operator

let Point { x: x1, y: y1 } = self.p1;

let Point { x: x2, y: y2 } = self.p2;

// `abs` is a `f64` method that returns the absolute value of the

// caller

((x1 - x2) * (y1 - y2)).abs()

}

fn perimeter(&self) -> f64 {

let Point { x: x1, y: y1 } = self.p1;

let Point { x: x2, y: y2 } = self.p2;

2.0 * ((x1 - x2).abs() + (y1 - y2).abs())

}

// This method requires the caller object to be mutable

// `&mut self` desugars to `self: &mut Self`

fn translate(&mut self, x: f64, y: f64) {

self.p1.x += x;

self.p2.x += x;

self.p1.y += y;

self.p2.y += y;

}

}

// `Pair` owns resources: two heap allocated integers

struct Pair(Box, Box);

impl Pair {

// This method "consumes" the resources of the caller object

// `self` desugars to `self: Self`

fn destroy(self) {

// Destructure `self`

let Pair(first, second) = self;

println!("Destroying Pair({}, {})", first, second);

// `first` and `second` go out of scope and get freed

}

}

fn main() {

let rectangle = Rectangle {

// Static methods are called using double colons

p1: Point::origin(),

p2: Point::new(3.0, 4.0),

};

// Instance method are called using the dot operator

// Note that the first argument `&self` is implicitly passed, i.e.

// `rectangle.perimeter()` === `perimeter(&rectangle)`

println!("Rectangle perimeter: {}", rectangle.perimeter());

println!("Rectangle area: {}", rectangle.area());

let mut square = Rectangle {

p1: Point::origin(),

p2: Point::new(1.0, 1.0),

};

// Error! `rectangle` is immutable, but this method requires a mutable

// object

//rectangle.translate(1.0, 0.0);

// TODO ^ Try uncommenting this line

// Ok, mutable object can call mutable methods

square.translate(1.0, 1.0);

let pair = Pair(Box::new(1), Box::new(2));

pair.destroy();

// Error! Previous `destroy` call "consumed" `pair`

//pair.destroy();

// TODO ^ Try uncommenting this line

}

¥ 我要打赏

纠错/补充

收藏

上一篇:

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值