// Test: Auto-clone for method call results
struct DataSource {
items: Vec<string>,
}
impl DataSource {
fn get_items(self) -> Vec<string> {
self.items.clone()
}
fn get_count(self) -> int {
self.items.len() as int
}
}
fn main() {
test_method_call_auto_clone()
test_chained_method_calls()
println!("✓ All method call auto-clone tests passed!")
}
fn test_method_call_auto_clone() {
let source = DataSource {
items: vec!["apple", "banana"],
}
// Move method result to function (should auto-clone)
let processed = process_items(source.get_items())
// source.get_items() should still be callable
assert!(source.get_items().len() == 2, "items should still have 2 elements")
assert!(processed == 2, "processed count should be 2")
println!("✓ Method call auto-clone works")
}
fn test_chained_method_calls() {
let source = DataSource {
items: vec!["a", "b", "c"],
}
// Use method result multiple times
let first = process_items(source.get_items())
let second = process_items(source.get_items())
let third = process_items(source.get_items())
assert!(first == 3, "first should be 3")
assert!(second == 3, "second should be 3")
assert!(third == 3, "third should be 3")
println!("✓ Chained method calls auto-clone works")
}
fn process_items(items: Vec<string>) -> int {
items.len() as int
}
fn assert(condition: bool, message: string) {
if !condition {
panic!("{}", message)
}
}