rust - 比较字符串和静态字符串

标签 rust rust-obsolete

这是我尝试过的示例。

static TARGET: &'static str = "a string";

fn main () {
  printfln!("%?", TARGET.eq(~"other string"));
}

我也查看了 equiv,但没有运气。我与 TARGET 比较的字符串必须是拥有的指针字符串。

最佳答案

这在这里有效:

static TARGET: &'static str = "a string";

fn main () {

  println!("{}", TARGET == "a string");
  println!("{}", TARGET == ~"a string");

  let other = ~"a string";
  println!("{}", TARGET == other);

}

它打印:

true
true
true

关于rust - 比较字符串和静态字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19599490/

相关文章:

rust - Rust 中的 "0is"表示法是什么?

rust - 如何编写一个转发到 vec 的可变 Rust 宏!宏?

rust - 局部变量的Rust异步问题

file - 无法将文件导入 Rust 中的另一个文件

rust - 构建 Rocket handlebars 示例时 Unresolved 导入模板

rust - "expected crate directive"使用rust 错误

rust - 如何将 Rust 中的范围转换为切片?

arrays - 使用常量表达式声明数组的大小

rust - vector 的借用引用的生命周期与其包含的借用指针之间有什么关系?