Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add rust usage example #2447

Merged
merged 6 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ debug = true

[workspace]
default-members = ["core"]
exclude = ["examples"]
members = [
"core",

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Major components of the project include:

## Documentation

The documentation is available at <https://opendal.apache.org>.
The documentation is available at <https://opendal.apache.org>. Checkout [here](./examples/) for the usage examples(still working on).
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved

We are engaged in a practice we call "documentation as code". You can also view the documentation directly in project's source code. And we welcome you to contribute to the documentation.

Expand Down
29 changes: 29 additions & 0 deletions examples/rust/section01-introduction/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

[package]
name = "section01-introduction"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
opendal = { version = "0.37.0" }
tokio = { version = "1.27", features = ["full"] }
anyhow = { version = "1.0.30", features = ["std"] }
futures = { version = "0.3" }
49 changes: 49 additions & 0 deletions examples/rust/section01-introduction/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Introduction

Rust is a systems programming language that is known for its performance, memory safety, and concurrency features. OpenDAL(**Open D**ata **A**ccess **L**ayer) is a Rust project to access data freely, painlessly and efficiently.

This examples documentation is organized in a progressive manner, with each section building upon the previous one. Each section is located in a different folder and contains both documentation and a stand-alone project that can be run independently.

The following will briefly introduce dev environment set up, the Rust project structure, some important files, and how to quickly create a project. This is prepared for those who are not familiar with Rust. If you are familiar with Rust, you can skip this section.


# Environment set up

There need rust toolchain in your local environment, if you haven't set it up, you can refer [here](https://github.com/apache/incubator-opendal/blob/main/CONTRIBUTING.md#bring-your-own-toolbox) to figure it out.


# Rust Project Structure

A typical Rust project follows a specific directory structure. Here's an overview of the main directories and files you'll encounter in a Rust project:

- src/: This directory contains the source code for your Rust project. It typically includes a main.rs file, which is the entry point for your application.

- Cargo.toml: This file contains metadata about your project, such as its name, version, and dependencies. It also specifies the build configuration for your project.

## Cargo.toml

Cargo is Rust's package manager and build tool. It uses the Cargo.toml file to manage dependencies and build configurations for your project.

Here's an example Cargo.toml file for a Rust project:

```toml
[package]
name = "my_project"
version = "0.1.0"
authors = ["Your Name <your.email@example.com>"]
edition = "2018"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
```
## main.rs
The main.rs file is the entry point for your Rust application. It typically contains the `main()` function, which is the starting point for your program.


# Basic Usage

In this section, we chose `Fs` service as example because it is simple and don't need extra work to set up, all service available can be found [here](https://docs.rs/opendal/latest/opendal/services/index.html).

This is a independent project, the `src/main.rs` provide a example usage about `Fs`. It `read` a file and print its content to console, overwrite some new content to it and read & print content again to verify the `write` operator has been taken effect and then delete the file.

Use `cargo run` to run this project. Just take a try.
57 changes: 57 additions & 0 deletions examples/rust/section01-introduction/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use opendal::Operator;
use opendal::Result;
use opendal::services::Fs;

#[tokio::main]
async fn main() -> Result<()> {

// Create fs backend builder.
let mut builder = Fs::default();

// set the root to `section01-introduction/tmp`, all operations will happen under this root.
// note that the root must be absolute path.
let mut path = std::env::current_dir().unwrap();
path.push("tmp");
builder.root(path.to_str().unwrap());

// create an `Operator` from `builder`, all file operations are initiated from it.
let op: Operator = Operator::new(builder)?.finish();

// if the 'root' path haven't been set, then the `file_path` below should be `section01-introduction/tmp/1.txt`.
let file_path = "1.txt";

// read the file and print its content.
let read_file = op.read(file_path).await?;
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
let content = String::from_utf8(read_file).unwrap();
println!("{}", content);

// write the file.
op.write(file_path, "File content has been overwrite.").await?;

// verify the file content after the write operator.
let read_file = op.read(file_path).await?;
let content = String::from_utf8(read_file).unwrap();
println!("{}", content);

// delete the file.
// op.delete(file_path).await?;

Ok(())
}
1 change: 1 addition & 0 deletions examples/rust/section01-introduction/tmp/1.txt
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is OpenDAL usage documentation section01-introduction.