Lv777

Lv777

github
twitter
jike
playstation
xiaoyuzhou

集合

向量#

Compared to [[Data Types#Arrays]], Vector is more like the concept of arrays in JS, allowing operations to add or remove elements, but it can still only store values of the same type.


let v: Vec<i32> = Vec::new();
let v = vec![1, 2, 3];

let num = v2.get(0); // Some(1)
let num = &v2[0];

// Traverse elements using for in
for num in &mut v {
	*num += 1
}

I wonder if it's possible to move vector elements through assignment statements. AI says it's possible, but the example code provided cannot be compiled, so I wrote an error message E0507 myself.

let v = vec![String::from(""), String::from("")];
let str = v[0];
//		  | move occurs because value has type `String`, which does not implement the `Copy` trait

Strings#

In Rust, strings generally refer to slice references &str, which are references to UTF-8 data stored somewhere. String is also a string, essentially a vector with additional restrictions and functionalities. str has a fixed length, while String exists on the heap and has mutable content.
Both can be converted to each other:

let mut s = String::from("str");
let r = s.as_str();

Modification#

let mut s = String::from("hello");
// Add a single character
s.push('_');
// Add a string
s.push_str("world");
// The right side of + can only be &str, and the ownership of the left side variable is transferred
let s = s + "_rust";
// Concatenation without taking ownership
let s1 = String::from("!");
let str = format!("{s}{s1}!!")

Hash Map#

The concept is similar to the Map in ES6, storing key-value pairs. The types of the key and value are fixed. When using it, be careful to avoid ownership issues and generally use references or clones.

use std::collections::HashMap;

let mut hash = HashMap::new();
hash.insert(String::from("x"), 1);

let x = hash.get(&String::from("x"));
// Insert default value based on existing value
let y = hash.entry(String::from("y")).or_insert(0);

I just realized that by default, Rust compares String based on their content rather than their address:

Lv777: If two Strings in Rust have the same content, are these two Strings equal?


Raycast AI: Yes, if two Rust strings have the same content, they are considered equal. This is because in Rust, strings are compared based on their content rather than their address. You can use the == operator to compare whether two strings are equal.


Lv777: What other types of comparison are based on content?


Raycast AI: In addition to string comparison in Rust, there are other types of comparison that are also based on their content rather than their address. For example, for arrays, slices, tuples, and other types in Rust, their comparison is also based on their content. You can use the == operator to compare whether they are equal.
Also, the comparison of structs and enum types in Rust is by default based on the values of their members, rather than their address. However, if the member types of a struct or enum type do not support comparison, you need to manually implement the PartialEq and Eq traits to support comparison operations.

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。