Movatterモバイル変換


[0]ホーム

URL:


codecamp

操作符和重载

操作符和重载

Rust 允许有限形式的操作符重载。有一些操作符能够被重载。为了支持一个类型之间特定的操作符,有一个你可以实现的特定的特征,然后重载操作符。

例如,可以用 Add 特征重载 + 操作符。

use std::ops::Add;#[derive(Debug)]struct Point {x: i32,y: i32,}impl Add for Point {type Output = Point;fn add(self, other: Point) -> Point {Point { x: self.x + other.x, y: self.y + other.y }}}fn main() {let p1 = Point { x: 1, y: 0 };let p2 = Point { x: 2, y: 3 };let p3 = p1 + p2;println!("{:?}", p3);}

在 main 函数中,你可以在两个 Point之间使用 + 操作符,因为我们可以使用 Point 的方法Add<Output=Point>

有许多操作符可以以这种方式被重载,所有的关联特征都在std::ops 模块中。看看完整列表的文档。    

这些特征的实现遵循一个模式。让我们看看 Add 的更多细节:

pub trait Add<RHS = Self> {type Output;fn add(self, rhs: RHS) -> Self::Output;}

这里总共三种类型包括:impl Add for的类型,默认为 Self 的 RSH,还有 Output。一个表达式let z = x + y,x 是 Self 类型,y 是 RSH 类型,还有 z 是 Self::Output 类型。

这可以让你这样做:

let p: Point = // ...let x: f64 = p + 2i32;
全类型
'Deref'强制转换
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录
关于
介绍

新手入门

学习 Rust

高效 Rust

语法和语义

Nightly Rust

词汇表
相关学术研究

关闭

MIP.setData({'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false},'pageFontSize' : getCookie('pageFontSize') || 20});MIP.watch('pageTheme', function(newValue){setCookie('pageTheme', JSON.stringify(newValue))});MIP.watch('pageFontSize', function(newValue){setCookie('pageFontSize', newValue)});function setCookie(name, value){var days = 1;var exp = new Date();exp.setTime(exp.getTime() + days*24*60*60*1000);document.cookie = name + '=' + value + ';expires=' + exp.toUTCString();}function getCookie(name){var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null;}
[8]ページ先頭

©2009-2025 Movatter.jp