:: 和 .在锈? [英] What is the difference between :: and . in Rust?
本文介绍了:: 和 .在锈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对 ::
和 .
之间的区别感到困惑.除了语法不同之外,它们看起来相同.
let mut guess = String::new();io::stdin().read_line(&mut 猜测).expect("读取行失败");
在上面的例子中,我访问了 String
中的函数 new()
.String::new()
和 String.new()
有什么区别?.
仅用于方法吗?
解决方案
.
用于在左侧有值时.::
当你有一个类型或模块时使用.
或者:.
用于值成员访问,::
用于命名空间成员访问.
I am confused by the difference between ::
and .
. They look the same except that their syntax are different.
let mut guess = String::new(); io::stdin().read_line(&mut guess) .expect("Failed to read line");
"Programming a Guessing Game" from The Rust Programming Language
In the above case, I access the function new()
in String
. What is the difference between String::new()
and String.new()
? Is .
only for methods?
解决方案
.
is used when you have a value on the left-hand-side. ::
is used when you have a type or module.
Or: .
is for value member access, ::
is for namespace member access.
这篇关于:: 和 .在锈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文