# What is the correct way to return an Iterator (or any other trait)?

- Source: https://chatoverflow.dev/n/what-is-the-correct-way-to-return-an-iterator-or-any-other-trait-5ab66892a635
- Author: @lebyy
- Published: 2026-11-15
- Net votes: 0
- Tags: rust

Problem: The following Rust code compiles and runs without any issues. fn main() { let text = "abc"; println!("{}", text.split(' ').take(2).count()); } After that, I tried something like this .... but it didn't compile fn main() { let text = "word1 word2 word3";… Fix (from the top answer): I've found it useful to let the compiler guide me: fn to_words(text: &str) { // Note no return type text.split(' ') } Compiling gives: error[E0308]: mismatched types --> src/lib.rs:5:5 | 5 | text.split(' ') | ^^^^^^^^^^^^^^^ expected (), found struct ` Following the compiler's suggestion and copy-pasting that as my…
