我对借贷和所有权感到困惑.在Rust 文档中有关引用和借用的内容
let mut x = 5;
{
let y = &mut x;
*y += 1;
}
println!("{}", x);
Run Code Online (Sandbox Code Playgroud)
他们说
println!可以借x.
我很困惑.如果println!借入x,为什么它通过x不&x?
我尝试在下面运行此代码
fn main() {
let mut x = 5;
{
let y = &mut x;
*y += 1;
}
println!("{}", &x);
}
Run Code Online (Sandbox Code Playgroud)
除了传递&x给代码之外,这段代码与上面的代码相同println!.它将'6'打印到控制台,这是正确的,与第一个代码的结果相同.
use std::collections::HashMap;
fn main() {
let mut hash = HashMap::new();
hash.insert("Daniel", "798-1364");
println!("{}", hash);
}
Run Code Online (Sandbox Code Playgroud)
不会编译错误
特征绑定std :: collections :: HashMap <&str,&str>:std :: fmt ::显示不满意
有没有办法说出类似的话:
error[E0277]: `std::collections::HashMap<&str, &str>` doesn't implement `std::fmt::Display`
--> src/main.rs:6:20
|
6 | println!("{}", hash);
| ^^^^ `std::collections::HashMap<&str, &str>` cannot be formatted with the default formatter
|
Run Code Online (Sandbox Code Playgroud)
打印出来:
println!("{}", hash.inspect());
Run Code Online (Sandbox Code Playgroud) 我很遗憾地问这么简单的问题......前一天,我开始学习Rust并尝试了这个println!方法.
fn main() {
println!("Hello {}!", "world");
}
-> Hello world!
Run Code Online (Sandbox Code Playgroud)
然后,我找到了其他格式样式:{}, {:}, {:?}, {?},...
我知道{}相反String,但我不理解其他格式风格.这些款式如何相互不同?我认为{:?}是数组或向量.这是对的吗?
请用示例代码解释这些格式样式:(