Index
- Guessing Game
- Common Programming Concepts
- Understanding Ownership
- Using Structs
- Enums and Pattern Matching
- Managing Growing Projects with Packages, Crates, and Modules
- Defining Modules to Control Scope and Privacy
- Paths for Referring to an Item in the Module Tree
- Bringing Paths into Scope with the use Keyword
- Separating Modules into Different Files
- Common Collections
- Error Handling
- Generic Types, Traits, and Lifetimes
- Writing Automated Tests
- Object Oriented Programming
- Adding dependancies
- Option Take
- RefCell
- mem
- Data Structure
- Recipe
- Semi colon
- Calling rust from python
- Default
- Crytocurrency With rust
- Function chaining
- Question Mark Operator
- Tests with println
- lib and bin
- Append vector to hash map
- Random Number
- uuid4
- uwrap and option
- Blockchain with Rust
- Near Protocol
- Actix-web
The Slice Type
The Slice Type
Another data type that does not have ownership is the slice. Slices let you reference a contiguous sequence of elements in a collection rather than the whole collection.
Inside the
for
loop, we search for the byte that represents the space by using the byte literal syntax. If we find a space, we return the position. Otherwise, we return the length of the string by using s.len()
fn first_word(s: &String) -> usize {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return i;
}
}
s.len()
}
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return i;
}
}
s.len()
}
String Slices
A string slice is a reference to part of a
String
, and it looks like this: let s = String::from("hello world");
let hello = &s[0..5];
let world = &s[6..11];
let hello = &s[0..5];
let world = &s[6..11];
This is similar to taking a reference to the whole
String
but with the extra [0..5]
bit. Rather than a reference to the entire String
, it’s a reference to a portion of the String
.We can create slices using a range within brackets by specifying
[starting_index..ending_index]
, where starting_index
is the first position in the slice and ending_index
is one more than the last position in the slice.![images/873-1.png](images/873-1.png)
With Rust’s
..
range syntax, if you want to start at the first index (zero), you can drop the value before the two periods. In other words, these are equal:let s = String::from("hello");
let slice = &s[0..2];
let slice = &s[..2];
let slice = &s[0..2];
let slice = &s[..2];
By the same token, if your slice includes the last byte of the
String
, you can drop the trailing number. That means these are equal:let s = String::from("hello");
let len = s.len();
let slice = &s[3..len];
let slice = &s[3..];
let len = s.len();
let slice = &s[3..len];
let slice = &s[3..];
You can also drop both values to take a slice of the entire string. So these are equal:
let s = String::from("hello");
let len = s.len();
let slice = &s[0..len];
let slice = &s[..];
let len = s.len();
let slice = &s[0..len];
let slice = &s[..];
String Literals Are Slices
Recall that we talked about string literals being stored inside the binary. Now that we know about slices, we can properly understand string literals:
let s = "Hello, world!";
The type of
s
here is &str
: it’s a slice pointing to that specific point of the binary. This is also why string literals are immutable; &str
is an immutable reference.fn main() {
let my_string = String::from("hello world");
// first_word works on slices of `String`s
let word = first_word(&my_string[..]);
let my_string_literal = "hello world";
// first_word works on slices of string literals
let word = first_word(&my_string_literal[..]);
// Because string literals *are* string slices already,
// this works too, without the slice syntax!
let word = first_word(my_string_literal);
}
let my_string = String::from("hello world");
// first_word works on slices of `String`s
let word = first_word(&my_string[..]);
let my_string_literal = "hello world";
// first_word works on slices of string literals
let word = first_word(&my_string_literal[..]);
// Because string literals *are* string slices already,
// this works too, without the slice syntax!
let word = first_word(my_string_literal);
}
Other Slices
let a = [1, 2, 3, 4, 5];
let a = [1, 2, 3, 4, 5];
let slice = &a[1..3];