Lv777

Lv777

github
twitter
jike
playstation
xiaoyuzhou

Gathering or assembly

Vector#

Compared to Array, Vector is more like the concept of arrays in JS. It allows for operations to add and remove elements, but it can 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 loop
for num in &mut v {
    *num += 1
}

Curious if it's possible to move vector elements through assignment statements. AI says it's possible, but the provided example code cannot be compiled, and it gives an error E0507.

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 functionality. str has a fixed length, while String is mutable and stored on the heap.
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");
// + operator can only have &str on the right side, ownership of the left 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 keys and values 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 Rust compares String based on their content by default, not their addresses:

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 addresses. You can use the == operator to compare two strings for equality.


Lv777: Are there any other types of comparisons that compare based on content?


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

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.