Converting error[E0608]: cannot index into a value of type `std::result::Result<std::string::String, Rexiv2Error> to string

Hello to all,
I am just relearning Rust in which I am implementing a concrete task.
The task is about renaming RAW images by means of
the exif data of the image.

I search the images with a certain extension and write them sorted
in a list.
Then I iterate over the list and determine for each element the Exif-
data for each element and show the Exif tag <Exif.Image.DateTime> via

So far it works fine. Now I want to get the result of println!()
Ok("2020:02:21 17:00:30"). The attempt to convert this result
into a "normal" string by means of

let zw = exif_datetime[2..];

leads to a compile error:

error[E0608]: cannot index into a value of type `std::result::Result<std::string::String, Rexiv2Error>`
  --> src/
42 | let zw = exif_datetime[2..];
   | ^^^^^^^^^^^^^^^^^^

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0608`.
error: could not compile `rust_args`.

I have already searched the internet for a solution to this problem, but I have
but did not find anything or did not read it (did not understand it).

Can you please help me?

Thanks for your help and tips.

See you then ...

Sorry for this English, it comes from a translator.

deutsche Version unten

I would recommend you to give a somewhat more complete code example. I presume, based on that error message, that you’re using the rexiv2 crate, however I’m not sure which function you used to create your variable exif_datetime or what you’re planning to do with it.

In general, the Result type in Rust is a way of error-handling, somewhat similar to exceptions in other programming languages. You can read more about error handling in the book—in general you usually want to access the contained type somehow; there’s multiple approaches for doing this, but the indexing syntax “[..2]” is not a way to do that. Also I’m not entirely sure what your problem has to do with println!.

Ich würde dir allgemein empfehlen, bei Fragen zu/mit Fehlermeldungen auch etwas ausführlichere Ausschnitte aus deinem Quellcode beizulegen. Vermutlich verwendest du (den Fehlermeldungen nach zu Urteilen) das rexiv2 Crate, allerdings weiß ich z.B. nicht, welche Funktion aus diesem Crate du wie benutzt um deine Variable exif_datetime zu definieren und was genau du mit dem Wert vor hast.

Grundsätzlich dient der Datentyp Result in Rust der Fehlerbehandlung, mehr oder weniger ähnlich zu Exceptions in einigen anderen Programmiersprachen. Du kannst mehr zum Thema Fehlerbehandlung im Rust-Buch (Link zur deutschen Übersetzung) nachlesen – normalerweise muss man sich bei Result auf die eine oder andere Weise Zugriff auf den enthaltenden Wert verschaffen; das geht auf mehrere verschiedene Arten, aber mit dem Indizierungs-Operator „[2..]“, den du verwendest, funktioniert das nicht. Ich verstehe außerdem nicht genau, was das ganze mit println! zu tun hat.


You haven't handled error when creating exif_datetime. It's not datetime, it's "maybe-datetime-or-maybe-error-we-don't-know" type. You need to use ? operator or match or if let or something else to convert Result to the type you actually need.

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.