fn main(){
let mydata = "hello world";
let object = &mydata[0..1];
let access = *object;
println!("{}", access);
}
Compiling playground v0.0.1 (/playground)
error[E0425]: cannot find value `pointer` in this scope
--> src/main.rs:5:19
|
5 | let access = *pointer;
| ^^^^^^^ not found in this scope
For more information about this error, try `rustc --explain E0425`.
error: could not compile `playground` due to previous error
why am i getting error ?
&(reference) : allows you to access the original data and modify a copy of it instead of modifying the actual data
*(dereference) : allows you to access the data the reference is pointing to
You should use the right name when accessing the variable.
fn main(){
let mydata = "hello world";
let object = &mydata[0..1];
- let access = *pointer;
+ let access = *object;
println!("{}", access);
}
However, this gives another error:
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> src/main.rs:6:9
|
6 | let access = *object;
| ^^^^^^ doesn't have a size known at compile-time
|
Now, the syntax *object is perfectly fine as a place-expression for accessing the string data — the expression is a way to talk about the memory location where the string data is stored.
However, when you use a place-expression as a value, then it must make sense to read the target value into a local variable. In your case, this is not possible since the string data could have any length, but local variables must store values of a fixed size.
To see that *object is a valid place expression, you could try something like this:
let access = (*object).len();
which compiles fine. (Note that due to how the dot operator works, this you can also just write object.len() to get the same effect.)
Compiling playground v0.0.1 (/playground)
error[E0425]: cannot find value `pointer` in this scope
--> src/main.rs:5:19
|
5 | let access = *pointer;
| ^^^^^^^ not found in this scope
For more information about this error, try `rustc --explain E0425`.
error: could not compile `playground` due to previous error
What ispointer? You use it, but don't define it anywhere. It even tells you to try rustc --explain E0425. People put a lot of work into helpful compiler errors, so please read them carefully.
The error shows different code than your post. If you're using the Rust playground webiste, use the Share option and paste the URL here.
If you are writing *pointer intentionally, then this is the error. You can't just use the name pointer. There is no such thing built-in in Rust, and it doesn't mean anything.
When you have defined variables using let, you must use their names like mydata, not pointer.
Maybe it will be easier to understand if we talk about the types.
fn main(){
let mydata: &str = "hello world";
let object: &str = &mydata[0..1];
let access: str = *object;
println!("{}", access);
}
mydata is a &str. That means it's a reference to a str.
object is also a &str. Up to this point, we have no problems.
The * operator means "dereference" it takes some &T and turns it into a T.
The problem occurs when we try to store a str instead of a &str. str is an unsized type. That means we don't always know how much space it takes in memory. There are certain limitations on what we can do with unsized types. One limitation is that we can't store them on the stack, so we can't ever have a variable of type str but can only have references: &str.
We can make your code compile by removing the dereference operator, the *. This will let us store another &str instead.
fn main(){
let mydata: &str = "hello world";
let object: &str = &mydata[0..1];
let access: &str = object;
println!("{}", access);
}
By the way, there is another version of str that is sized. It's called String. Since String is sized, we can store it directly in a variable without taking a reference.
fn main(){
let mydata: &str = "hello world";
let object: &str = &mydata[0..1];
let access: String = object.to_string();
println!("{}", access);
}
string,str,&str is different,
str is immutable utf8 bytes and dynamic length, since can not use directly, should use it behind pointer,
&str is slice that like a view of str, contains len property.
string like &str,but is fat pointer ,conains len and cap property