Why i cannot use * and & both?

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 

am i wrong ?

Please post your complete code. For example, you current example does not include the definition of pointer.

7 Likes

my code is complete what should i include ?

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.)

4 Likes

the problem still persist

Elaborate? Did you read my entire answer?

4 Likes

yes but i still get the error

Please copy and paste your full program after the change, and the full error as well.

Your error message says everything.

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 is pointer? 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.

6 Likes

This isn't accurate, I recommend reading the Rust book, in particular the Understanding Ownership and Treating Smart Pointers Like Regular References with the Deref Trait sections which go over ownership, & and *.

3 Likes

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.

4 Likes

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);
}
2 Likes

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

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.