Skip to content

Commit a9effb4

Browse files
committed
Make examples consistent
1 parent e13cca9 commit a9effb4

File tree

3 files changed

+49
-59
lines changed

3 files changed

+49
-59
lines changed

src/lib.rs

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ pub struct CancelData {
268268
/// thread::spawn(move || {
269269
/// conn.execute("SOME EXPENSIVE QUERY", &[]).unwrap();
270270
/// });
271-
/// # let _ =
272-
/// postgres::cancel_query(url, &SslMode::None, cancel_data);
271+
/// postgres::cancel_query(url, &SslMode::None, cancel_data).unwrap();
273272
/// ```
274273
pub fn cancel_query<T>(params: T,
275274
ssl: &SslMode,
@@ -873,25 +872,24 @@ impl Connection {
873872
/// # Examples
874873
///
875874
/// ```rust,no_run
876-
/// # use postgres::{Connection, SslMode};
877-
/// # fn f() -> Result<(), ::postgres::error::ConnectError> {
875+
/// use postgres::{Connection, SslMode};
876+
///
878877
/// let url = "postgresql://postgres:hunter2@localhost:2994/foodb";
879-
/// let conn = try!(Connection::connect(url, &SslMode::None));
880-
/// # Ok(()) };
878+
/// let conn = Connection::connect(url, &SslMode::None).unwrap();
881879
/// ```
882880
///
883881
/// ```rust,no_run
884-
/// # use postgres::{Connection, SslMode};
885-
/// # fn f() -> Result<(), ::postgres::error::ConnectError> {
882+
/// use postgres::{Connection, SslMode};
883+
///
886884
/// let url = "postgresql://postgres@%2Frun%2Fpostgres";
887-
/// let conn = try!(Connection::connect(url, &SslMode::None));
888-
/// # Ok(()) };
885+
/// let conn = Connection::connect(url, &SslMode::None).unwrap();
889886
/// ```
890887
///
891888
/// ```rust,no_run
892-
/// # use postgres::{Connection, UserInfo, ConnectParams, SslMode, ConnectTarget};
889+
/// use postgres::{Connection, UserInfo, ConnectParams, SslMode, ConnectTarget};
890+
///
893891
/// # #[cfg(feature = "unix_socket")]
894-
/// # fn f() -> Result<(), ::postgres::error::ConnectError> {
892+
/// # fn f() {
895893
/// # let some_crazy_path = Path::new("");
896894
/// let params = ConnectParams {
897895
/// target: ConnectTarget::Unix(some_crazy_path),
@@ -903,8 +901,8 @@ impl Connection {
903901
/// database: None,
904902
/// options: vec![],
905903
/// };
906-
/// let conn = try!(Connection::connect(params, &SslMode::None));
907-
/// # Ok(()) };
904+
/// let conn = Connection::connect(params, &SslMode::None).unwrap();
905+
/// # }
908906
/// ```
909907
pub fn connect<T>(params: T, ssl: &SslMode) -> result::Result<Connection, ConnectError>
910908
where T: IntoConnectParams
@@ -937,12 +935,14 @@ impl Connection {
937935
///
938936
/// ```rust,no_run
939937
/// # use postgres::{Connection, SslMode};
938+
/// # let x = 10i32;
940939
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
941-
/// let maybe_stmt = conn.prepare("SELECT foo FROM bar WHERE baz = $1");
942-
/// let stmt = match maybe_stmt {
943-
/// Ok(stmt) => stmt,
944-
/// Err(err) => panic!("Error preparing statement: {:?}", err)
945-
/// };
940+
/// let stmt = conn.prepare("SELECT foo FROM bar WHERE baz = $1").unwrap();
941+
/// for row in stmt.query(&[&x]).unwrap() {
942+
/// let foo: String = row.get(0);
943+
/// println!("foo: {}", foo);
944+
/// }
945+
/// ```
946946
pub fn prepare<'a>(&'a self, query: &str) -> Result<Statement<'a>> {
947947
self.conn.borrow_mut().prepare(query, self)
948948
}
@@ -958,14 +958,13 @@ impl Connection {
958958
///
959959
/// ```rust,no_run
960960
/// # use postgres::{Connection, SslMode};
961-
/// # fn f() -> postgres::Result<()> {
962961
/// # let x = 10i32;
963962
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
964-
/// let stmt = try!(conn.prepare_cached("SELECT foo FROM bar WHERE baz = $1"));
965-
/// for row in try!(stmt.query(&[&x])) {
966-
/// println!("foo: {}", row.get::<_, String>(0));
963+
/// let stmt = conn.prepare_cached("SELECT foo FROM bar WHERE baz = $1").unwrap();
964+
/// for row in stmt.query(&[&x]).unwrap() {
965+
/// let foo: String = row.get(0);
966+
/// println!("foo: {}", foo);
967967
/// }
968-
/// # Ok(()) };
969968
/// ```
970969
pub fn prepare_cached<'a>(&'a self, query: &str) -> Result<Statement<'a>> {
971970
self.conn.borrow_mut().prepare_cached(query, self)
@@ -989,15 +988,12 @@ impl Connection {
989988
///
990989
/// ```rust,no_run
991990
/// # use postgres::{Connection, SslMode};
992-
/// # fn foo() -> Result<(), postgres::error::Error> {
993991
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
994-
/// let trans = try!(conn.transaction());
995-
/// try!(trans.execute("UPDATE foo SET bar = 10", &[]));
992+
/// let trans = conn.transaction().unwrap();
993+
/// trans.execute("UPDATE foo SET bar = 10", &[]).unwrap();
996994
/// // ...
997995
///
998-
/// try!(trans.commit());
999-
/// # Ok(())
1000-
/// # }
996+
/// trans.commit().unwrap();
1001997
/// ```
1002998
pub fn transaction<'a>(&'a self) -> Result<Transaction<'a>> {
1003999
let mut conn = self.conn.borrow_mut();
@@ -1074,23 +1070,22 @@ impl Connection {
10741070
/// # Example
10751071
///
10761072
/// ```rust,no_run
1077-
/// # use postgres::{Connection, Result};
1078-
/// fn init_db(conn: &Connection) -> Result<()> {
1079-
/// conn.batch_execute("
1080-
/// CREATE TABLE person (
1081-
/// id SERIAL PRIMARY KEY,
1082-
/// name NOT NULL
1083-
/// );
1073+
/// # use postgres::{Connection, SslMode, Result};
1074+
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
1075+
/// conn.batch_execute("
1076+
/// CREATE TABLE person (
1077+
/// id SERIAL PRIMARY KEY,
1078+
/// name NOT NULL
1079+
/// );
10841080
///
1085-
/// CREATE TABLE purchase (
1086-
/// id SERIAL PRIMARY KEY,
1087-
/// person INT NOT NULL REFERENCES person (id),
1088-
/// time TIMESTAMPTZ NOT NULL,
1089-
/// );
1081+
/// CREATE TABLE purchase (
1082+
/// id SERIAL PRIMARY KEY,
1083+
/// person INT NOT NULL REFERENCES person (id),
1084+
/// time TIMESTAMPTZ NOT NULL,
1085+
/// );
10901086
///
1091-
/// CREATE INDEX ON purchase (time);
1092-
/// ")
1093-
/// }
1087+
/// CREATE INDEX ON purchase (time);
1088+
/// ").unwrap();
10941089
/// ```
10951090
pub fn batch_execute(&self, query: &str) -> Result<()> {
10961091
self.conn.borrow_mut().quick_query(query).map(|_| ())

src/rows.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,12 @@ impl<'a> Row<'a> {
225225
/// ```rust,no_run
226226
/// # use postgres::{Connection, SslMode};
227227
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
228-
/// # let stmt = conn.prepare("").unwrap();
229-
/// # let mut result = stmt.query(&[]).unwrap();
230-
/// # let row = result.iter().next().unwrap();
231-
/// let foo: i32 = row.get(0);
232-
/// let bar: String = row.get("bar");
228+
/// let stmt = conn.prepare("SELECT foo, bar from BAZ").unwrap();
229+
/// for row in stmt.query(&[]).unwrap() {
230+
/// let foo: i32 = row.get(0);
231+
/// let bar: String = row.get("bar");
232+
/// println!("{}: {}", foo, bar);
233+
/// }
233234
/// ```
234235
pub fn get<I, T>(&self, idx: I) -> T
235236
where I: RowIndex + fmt::Debug + Clone,

src/stmt.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,8 @@ impl<'conn> Statement<'conn> {
159159
/// # let bar = 1i32;
160160
/// # let baz = true;
161161
/// let stmt = conn.prepare("UPDATE foo SET bar = $1 WHERE baz = $2").unwrap();
162-
/// match stmt.execute(&[&bar, &baz]) {
163-
/// Ok(count) => println!("{} row(s) updated", count),
164-
/// Err(err) => println!("Error executing query: {:?}", err)
165-
/// }
162+
/// let rows_updated = stmt.execute(&[&bar, &baz]).unwrap();
163+
/// println!("{} rows updated", rows_updated);
166164
/// ```
167165
pub fn execute(&self, params: &[&ToSql]) -> Result<u64> {
168166
check_desync!(self.conn);
@@ -231,11 +229,7 @@ impl<'conn> Statement<'conn> {
231229
/// # let conn = Connection::connect("", &SslMode::None).unwrap();
232230
/// let stmt = conn.prepare("SELECT foo FROM bar WHERE baz = $1").unwrap();
233231
/// # let baz = true;
234-
/// let rows = match stmt.query(&[&baz]) {
235-
/// Ok(rows) => rows,
236-
/// Err(err) => panic!("Error running query: {:?}", err)
237-
/// };
238-
/// for row in &rows {
232+
/// for row in stmt.query(&[&baz]).unwrap() {
239233
/// let foo: i32 = row.get("foo");
240234
/// println!("foo: {}", foo);
241235
/// }

0 commit comments

Comments
 (0)