rust - 应该首选将字符串文字转换为字符串
问题描述
在 Rust 中,有几种方法可以从字符串文字创建字符串:
fn main() {
let s_from = String::from("string"); // type on the right of the operator
let s_into: String = "string".into(); // type on the left of the operator
let s_to_string = "string".to_string(); // expresses type
let s_to_owned = "string".to_owned(); // expresses ownership
assert_eq!(s_from, s_into);
assert_eq!(s_from, s_to_string);
assert_eq!(s_from, s_to_owned);
}
- 是否有规则遵循与操作员相关的阅读方向?
- 有理由支持
From
/Into
胜过to_string()
/to_owned()
吗? - 是否有理由偏爱其中一个而不是其他所有?
随着几个开发人员在一个项目上工作,会发生这些混合使用。
解决方案
推荐阅读
- react-native - React-Native 错误 - VirtualizedLists 永远不应嵌套在具有相同方向的普通 ScrollViews 中
- python-3.x - 列出 azure 文件存储中的文件并获取最新文件
- flutter - Dart Hive 使用 Set 导致未知类型错误
- google-tag-manager - 如何使用谷歌标签管理器将自定义 html 实现到正文中
- java - 为什么它说无法解析符号身份验证?
- php - 在 PHP 中减去两个时钟时间
- c++ - 预期的不合格 ID 错误?另外我如何运行程序?
- postgresql - golang-migrate:将 loca CSVl 文件复制到远程 Postgress
- flutter - 如何在 Flutter/Dart 中合并三个列表?
- python - 'PipelineModel' 对象没有属性 'predict'