当前位置:网站首页>The difference between function and method in rust
The difference between function and method in rust
2022-07-23 17:47:00 【pilaf1990】
In programming languages , In particular, it supports both process oriented style , It also supports object-oriented programming languages , There are usually two nouns, function and method , Used to represent an executable named code block .
Function English is function, Method English is method.
stay Java in , Except for the basic types int、long、double etc. , Everything is the object , Objects have methods .Java There is no function in . Even if lambda The function like things in the expression are just syntax sugar , The method of anonymous object is still used behind . stay Java Methods cannot be passed directly in , The object must be passed .
stay C In language , All of them complete logical processing through functions , Functions don't need to be attached to anyone , It is a first-class citizen , You can pass function pointers around .
stay Rust in , At the same time, there are the concepts of function and method .Rust The functions in and C Similar in language ,Rust Methods and methods in Java Is similar to , It is generally defined in the encapsulated data struct、tuple、enum Above .
For example, the following code can clearly understand the difference between functions and methods :
fn main() {
let a = 10;
let b = 40;
println!("function add result:{}",add(a,b));
let computer = Computer{
a,
b,
};
println!("method add result:{}",computer.add());
}
// function function ( Process oriented style , similar C Functions in language )
fn add(a:i32,b:i32) ->i32{
a+b
}
struct Computer{
a:i32,
b:i32,
}
impl Computer {
// Method method ( Object oriented style , similar Java Object method in )
pub fn add(&self) ->i32{
self.a+self.b
}
}
边栏推荐
- [JS] check whether the date object is invalid date
- LeetCode_动态规划_中等_120.三角形最小路径和
- 不掌握这些坑,你敢用BigDecimal吗?
- xlinx pcie xvc
- 网页基础模版
- 别再问我MySQL为啥没走索引?就这几种原因,全都告诉你
- [operation] Yan Yi (Internet new technology operation)
- Redis distributed lock, it's really impossible without it
- Use Preparedstatement to select and display recorded JDBC programs
- JS tool CECP
猜你喜欢

工業物聯網中的時序數據

为啥一问 JVM 就 懵B ???

In depth understanding of USB communication protocol

Food safety | eight things you must know when choosing probiotic products

rust中的静态分发和动态分发

分析optimism重放合约地址攻击事件
A series of specifications of oneplus 10t were disclosed before the product was released

使用moment获取当天日期与下一天

Food safety eating preserved eggs will lead poisoning? Don't worry about eating after knowing these points

MySQL7种JOIN(图)
随机推荐
From Markov chain to GPT, Li Hang, director of ByteDance AI Lab, detailed the past and present lives of language models
[introduction series of redis] data types and related commands of redis
文献学习(part100)--An Introduction to Autoencoders
Single cell thesis record (part19) -- a comprehensive comparison on cell type composition information for St data
Makefile common functions notdir, wildcard, patsubst
LeetCode_455_分发饼干
【作业】研一(互联网新技术作业)
ContextLoaderListener vs DispatcherServlet
Food safety | what is the origin of plant meat that sounds very healthy?
Redis分布式锁,没它真不行
训练和测试的loss不下降,并且精度超低
Kubernetes kubelet hard core knowledge architecture
Summary of stock historical data download interface (dynamic update)
Analyze optimism replay contract address attack events
WSUS can patch MySQL Middleware_ Join the WSUS patch server and download the patch
Three things programmers want to do most | comics
Food safety chocolate is also true or false? How much do you know about it
Kv260 single board PS control setting IIC switch chip
The use method of quota command is expanded in detail, and the carrying method of quota command in RHEL 7! Disk capacity quota!
工业物联网中的时序数据