struct SemanticDirection;

fn main() {}
warning: struct is never used: `SemanticDirection`
 --> src/main.rs:1:1
  |
1 | struct SemanticDirection;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(dead_code)] on by default

我将在任何严重的情况下重新打开这些警告,但我只是在修补语言,这让我很抓狂。

我尝试添加#[允许(dead_code)]到我的代码,但这并不管用。

我如何连接以下类型的组合:

STR和STR String和str 字符串和字符串

我实现了以下方法和单元测试:

use std::fs::File;
use std::path::Path;
use std::io::prelude::*;

fn read_file(path: &Path) {
    let mut file = File::open(path).unwrap();
    let mut contents = String::new();
    file.read_to_string(&mut contents).unwrap();
    println!("{}", contents);
}

#[test]
fn test_read_file() {
    let path = &Path::new("/etc/hosts");
    println!("{:?}", path);
    read_file(path);
}

我以这样的方式运行单元测试:

rustc --test app.rs; ./app

我也可以运行这个

cargo test

我得到一条消息,说测试通过了,但是println!永远不会显示在屏幕上。为什么不呢?

为什么Rust有String和str?String和str之间有什么区别?什么时候使用String而不是str,反之亦然?其中一个被弃用了吗?