structCircle { x:f64, y:f64, radius:f64,}implHasAreaforCircle {fnarea(&self) ->f64 { std::f64::consts::PI * (self.radius * self.radius) }}fnmain() {let c =Circle { x:0.0f64, y:0.0f64, radius:1.0f64, };println!("circle c has an area of {}", c.area());}
letmut f = std::fs::File::open("foo.txt").ok().expect("Couldn’t open foo.txt");let buf =b"whatever"; // buf: &[u8; 8]let result = f.write(buf);# result.unwrap();
这里是错误:
error: type `std::fs::File` does not implement any method in scope named `write`
let result = f.write(buf);
^~~~~~~~~~
我们需要先use这个Write trait:
use std::io::Write;letmut f = std::fs::File::open("foo.txt").expect("Couldn’t open foo.txt");let buf =b"whatever";let result = f.write(buf);# result.unwrap(); // ignore the error