Rust中struct的function与method
一个示例就能看明白,关键处皆有注释,大致要点:impl 一个struct时,
1.如果方法参数为&self,则为方法 ,可以用"对象实例.方法"来调用
2.如果方法参数不是&self,则为函数,只能用"struct名::函数名"来调用
//类似java里的pojo类
struct Pet{
name:String,
age:i8, //最后1个,不能省略
} impl Pet{
//定义函数(类似java里的static方法)
fn to_string(pet:&Pet)->String{
format!("name:{},age:{}",pet.name,pet.age)
} //定义方法(类似java里的实例方法)
fn to_str(&self)->String{
format!("name:{},age:{}",self.name,self.age)
}
} fn main() {
let dog = Pet{
name:String::from("dog"),
age:1
};
//调用Pet里的function
println!("{}",Pet::to_string(&dog));
//调用Pet实例dog上的method
println!("{}",dog.to_str());
}
输出:
name:dog,age:1
name:dog,age:1
Rust中struct的function与method的更多相关文章
- Myeclipse10中出现Cannot return from outside a function or method错误提示
最近发现myeclipse10中有几处bug 比如: Cannot return from outside a function or method onClick="return chec ...
- 你真的了解PeopleSoft中的function和method方法嘛
谈下function和method在内嵌与外部传参的区别 1.内嵌函数(Internal Functions) 看下现在输出&x的话会返回什么值? 2.内嵌函数(Internal Functi ...
- jsp中出现onclick函数提示Cannot return from outside a function or method
在使用Myeclipse10部署完项目后,原先不出错的项目,会有红色的叉叉,JSP页面会提示onclick函数错误 Cannot return from outside a function or m ...
- [Go语言]从Docker源码学习Go——function和method
function和method关系 method是针对某一类型定义的function, function可以单独调用,method必须针对某一类型的实例进行调用 //function 调用方式 pac ...
- 【译】理解Rust中的闭包
原文标题:Understanding Closures in Rust 原文链接:https://medium.com/swlh/understanding-closures-in-rust-21f2 ...
- 【译】理解Rust中的Futures(二)
原文标题:Understanding Futures in Rust -- Part 2 原文链接:https://www.viget.com/articles/understanding-futur ...
- Cannot return from outside a function or method
最近发现myeclipse10中有几处bug 比如: Cannot return from outside a function or method onClick="return chec ...
- myeclipse 10 载入新的项目报错Cannot return from outside a function or method
myeclipse 10 载入新的项目报错Cannot return from outside a function or method 解决方法: 方法一: window -->prefere ...
- Rust 中的继承与代码复用
在学习Rust过程中突然想到怎么实现继承,特别是用于代码复用的继承,于是在网上查了查,发现不是那么简单的. C++的继承 首先看看c++中是如何做的. 例如要做一个场景结点的Node类和一个Sprit ...
- drp错误集锦---“Cannot return from outside a function or method”
好久都不动的项目,今天打开项目突然是红色感叹号.详细错误表现为: 也就是说,如今MyEclipse已经不识别在JSP页面中使用的return方法了(并且不止一处这种警告),那怎么办?????顿时闹钟一 ...
随机推荐
- vue中 <el-table-column>回显转成百分比【数字转为百分比】
一.方案1[不保留小数] 这里直接乘以100然后加入百分号既可 <el-table-column prop="refundRate15" label="15天退款率 ...
- OSS 上传和删除图片 Python SDK
最近在搞一些全栈的小项目, 涉及到图片的上传, 删改等操作, 想着还是用这个云服务器来整一波, 阿里云的 OSS对象存储, 40G 1年9块钱, 值得拥有! 前提 购买了阿里云 OSS 服务 创建了 ...
- np.random总结
import numpy as np (1)np.random.random_sample help(np.random.random_sample) Help on built-in functio ...
- SQL注入入门实例讲解(NewsCenter
今天写了个sql注入的题目(也是跟着教程做的hhh),过程中有些心得体会,想着写篇博客总结一下. 笔者也是初学者,如有哪里讲错了,欢迎大佬在评论区点出 题目来源:攻防世界web进阶区,NewsCent ...
- CommonJS、ES 导出和导入模块
以下代码制作展示,不能直接运行. CommonJS导出 // module.cjs // CJS默认导出 //module.exports = 'Hello world'; /*module.expo ...
- Visio画图心得
关于Visio画图时的心得 1.关于对齐 我之前常常是在visio中视图里打开网格,然后根据网格来对齐框框,但是其实网格旁边的参考线用于对齐更好用. 首先,打开在视图选项卡里勾上参考线. 然后因为要根 ...
- 【中英】【吴恩达课后测验】Course 5 - 序列模型 - 第一周测验
[中英][吴恩达课后测验]Course 5 -序列模型 - 第一周测验 - 循环神经网络 上一篇:[课程4 - 第四周编程作业]※※※※※ [回到目录]※※※※※下一篇:[待撰写-课程5 -第一周编程 ...
- java中的mysql事务
mysql事务 如何进入事务处理? 开启事务 start transaction; 执行语句 增加.修改.删除 等业务处理的sql语句... 回滚事务 rollback; 相当于sql语句都没有执行 ...
- javascript定义函数后立即执行(IIFE)
Talk is cheap, show me the code. // Immediately Invoked Function Expression - IIFE // 定义后立即执行的JavaSc ...
- UFT RegExp